Skip to content

Commit

Permalink
feat(color-palettes): add opt arg to get theme in reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 23, 2023
1 parent a86c25e commit 4163fda
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions packages/color-palettes/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { BINARY, NUM_THEMES } from "./binary.js";
import { compFilter } from "./filter.js";

/**
* Returns theme for given ID as CSS hex colors.
* Returns theme for given ID as CSS hex colors. If `reverse` is true (default:
* false), the theme colors will be returned in reverse order.
*
* @param id
* @param reverse
*/
export const asCSS = (id: number) => {
export const asCSS = (id: number, reverse = false) => {
__ensureID(id);
const theme: CSSTheme = [];
// (<any>theme).__id = id;
Expand All @@ -29,16 +31,18 @@ export const asCSS = (id: number) => {
U24((BINARY[id] << 16) | (BINARY[id + 1] << 8) | BINARY[id + 2])
);
}
return theme;
return reverse ? theme.reverse() : theme;
};

/**
* Returns theme for given ID as packed ARGB integers (alpha channel will always
* be set to 0xff).
* be set to 0xff). If `reverse` is true (default: false), the theme colors will
* be returned in reverse order.
*
* @param id
* @param reverse
*/
export const asInt = (id: number) => {
export const asInt = (id: number, reverse = false) => {
__ensureID(id);
const theme: IntTheme = [];
id *= 18;
Expand All @@ -51,22 +55,27 @@ export const asInt = (id: number) => {
0
);
}
return theme;
return reverse ? theme.reverse() : theme;
};

/**
* Returns theme for given ID as thi.ng/color LCH color vectors.
* Returns theme for given ID as thi.ng/color LCH color vectors. If `reverse` is
* true (default: false), the theme colors will be returned in reverse order.
*
* @param id
* @param reverse
*/
export const asLCH = (id: number): LCHTheme => asRGB(id).map((x) => lch(x));
export const asLCH = (id: number, reverse = false): LCHTheme =>
asRGB(id, reverse).map((x) => lch(x));

/**
* Returns theme for given ID as thi.ng/color sRGB color vectors.
* Returns theme for given ID as thi.ng/color sRGB color vectors. If `reverse`
* is true (default: false), the theme colors will be returned in reverse order.
*
* @param id
* @param reverse
*/
export const asRGB = (id: number) => {
export const asRGB = (id: number, reverse = false) => {
__ensureID(id);
const theme: RGBTheme = <any>[];
// (<any>theme).__id = id;
Expand All @@ -81,7 +90,7 @@ export const asRGB = (id: number) => {
)
);
}
return theme;
return reverse ? theme.reverse() : theme;
};

/**
Expand Down

0 comments on commit 4163fda

Please sign in to comment.