@digdir/designsystemet 1.0.0-next.41 → 1.0.0-next.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/bin/designsystemet.js +18 -13
  2. package/dist/src/colors/luminance.d.ts +54 -0
  3. package/dist/src/colors/luminance.d.ts.map +1 -0
  4. package/dist/src/colors/luminance.js +56 -0
  5. package/dist/src/colors/theme.d.ts +14 -68
  6. package/dist/src/colors/theme.d.ts.map +1 -1
  7. package/dist/src/colors/theme.js +50 -224
  8. package/dist/src/colors/types.d.ts +23 -2
  9. package/dist/src/colors/types.d.ts.map +1 -1
  10. package/dist/src/colors/utils.d.ts +41 -50
  11. package/dist/src/colors/utils.d.ts.map +1 -1
  12. package/dist/src/colors/utils.js +91 -184
  13. package/dist/src/tokens/build/configs.d.ts.map +1 -1
  14. package/dist/src/tokens/build/configs.js +1 -1
  15. package/dist/src/tokens/build/formats/css.js +1 -1
  16. package/dist/src/tokens/build/formats/js-tokens.js +1 -1
  17. package/dist/src/tokens/build/transformers.js +1 -1
  18. package/dist/src/tokens/build/types.d.ts +1 -0
  19. package/dist/src/tokens/build/types.d.ts.map +1 -1
  20. package/dist/src/tokens/build/utils/entryfile.d.ts +1 -0
  21. package/dist/src/tokens/build/utils/entryfile.d.ts.map +1 -1
  22. package/dist/src/tokens/build/utils/entryfile.js +3 -2
  23. package/dist/src/tokens/build.d.ts +3 -1
  24. package/dist/src/tokens/build.d.ts.map +1 -1
  25. package/dist/src/tokens/build.js +16 -11
  26. package/dist/src/tokens/create.js +12 -12
  27. package/dist/src/tokens/types.d.ts +1 -1
  28. package/dist/src/tokens/types.d.ts.map +1 -1
  29. package/dist/src/tokens/{build/utils/utils.d.ts → utils.d.ts} +4 -0
  30. package/dist/src/tokens/utils.d.ts.map +1 -0
  31. package/dist/src/tokens/{build/utils/utils.js → utils.js} +35 -1
  32. package/dist/src/tokens/write/generate$metadata.d.ts +3 -3
  33. package/dist/src/tokens/write/generate$metadata.d.ts.map +1 -1
  34. package/dist/src/tokens/write/generate$metadata.js +4 -4
  35. package/dist/src/tokens/write/generate$themes.d.ts +2 -2
  36. package/dist/src/tokens/write/generate$themes.d.ts.map +1 -1
  37. package/dist/src/tokens/write.d.ts +2 -1
  38. package/dist/src/tokens/write.d.ts.map +1 -1
  39. package/dist/src/tokens/write.js +23 -21
  40. package/package.json +9 -5
  41. package/dist/src/tokens/build/actions.d.ts +0 -6
  42. package/dist/src/tokens/build/actions.d.ts.map +0 -1
  43. package/dist/src/tokens/build/actions.js +0 -33
  44. package/dist/src/tokens/build/utils/utils.d.ts.map +0 -1
@@ -9,19 +9,23 @@ import { writeTokens } from "../src/tokens/write.js";
9
9
  program.name("designsystemet").description("CLI for working with Designsystemet").showHelpAfterError();
10
10
  function makeTokenCommands() {
11
11
  const tokenCmd = createCommand("tokens");
12
- const DEFAULT_TOKENSDIR = "./design-tokens";
13
- tokenCmd.command("build").description("Build Designsystemet tokens").option("-t, --tokens <string>", `Path to ${chalk.blue("design-tokens")}`, DEFAULT_TOKENSDIR).option("-o, --out <string>", `Output directory for built ${chalk.blue("design-tokens")}`, "./build").option("-p, --preview", "Generate preview token.ts files", false).option("--verbose", "Enable verbose output", false).action((opts) => {
14
- const tokens = typeof opts.tokens === "string" ? opts.tokens : DEFAULT_TOKENSDIR;
15
- const out = typeof opts.out === "string" ? opts.out : "./dist/tokens";
16
- const preview = opts.preview;
17
- const verbose = opts.verbose;
12
+ const DEFAULT_TOKENS_DIR = "./design-tokens";
13
+ const DEFAULT_BUILD_DIR = "./design-tokens-build";
14
+ tokenCmd.command("build").description("Build Designsystemet tokens").option("-t, --tokens <string>", `Path to ${chalk.blue("design-tokens")}`, DEFAULT_TOKENS_DIR).option("-o, --out-dir <string>", `Output directory for built ${chalk.blue("design-tokens")}`, DEFAULT_BUILD_DIR).option("--dry [boolean]", `Dry run for built ${chalk.blue("design-tokens")}`, false).option("-p, --preview", "Generate preview token.ts files", false).option("--verbose", "Enable verbose output", false).action((opts) => {
15
+ const { preview, verbose } = opts;
16
+ const tokens = typeof opts.tokens === "string" ? opts.tokens : DEFAULT_TOKENS_DIR;
17
+ const outDir = typeof opts.outDir === "string" ? opts.outDir : "./dist/tokens";
18
+ const dry = Boolean(opts.dry);
18
19
  console.log(`Building tokens in ${chalk.green(tokens)}`);
19
- return buildTokens({ tokens, out, preview, verbose });
20
+ if (dry) {
21
+ console.log(`Performing dry run, no files will be written`);
22
+ }
23
+ return buildTokens({ tokens, outDir, preview, verbose, dry });
20
24
  });
21
- tokenCmd.command("create").description("Create Designsystemet tokens").requiredOption(`-m, --${colorCliOptions.main} <name:hex...>`, `Main colors`, parseColorValues).requiredOption(`-s, --${colorCliOptions.support} <name:hex...>`, `Support colors`, parseColorValues).requiredOption(`-n, --${colorCliOptions.neutral} <hex>`, `Neutral hex color`, convertToHex).option("-w, --write [string]", `Output directory for created ${chalk.blue("design-tokens")}`, DEFAULT_TOKENSDIR).option("-f, --font-family <string>", `Font family`, "Inter").option("--theme <string>", `Theme name`, "theme").action(async (opts) => {
22
- const { theme, fontFamily } = opts;
25
+ tokenCmd.command("create").description("Create Designsystemet tokens").requiredOption(`-m, --${colorCliOptions.main} <name:hex...>`, `Main colors`, parseColorValues).requiredOption(`-s, --${colorCliOptions.support} <name:hex...>`, `Support colors`, parseColorValues).requiredOption(`-n, --${colorCliOptions.neutral} <hex>`, `Neutral hex color`, convertToHex).option("-o, --out-dir <string>", `Output directory for created ${chalk.blue("design-tokens")}`, DEFAULT_TOKENS_DIR).option("--dry [boolean]", `Dry run for created ${chalk.blue("design-tokens")}`, false).option("-f, --font-family <string>", `Font family`, "Inter").option("--theme <string>", `Theme name`, "theme").action(async (opts) => {
26
+ const { theme, fontFamily, outDir } = opts;
27
+ const dry = Boolean(opts.dry);
23
28
  console.log(`Creating tokens with options ${chalk.green(JSON.stringify(opts, null, 2))}`);
24
- const write = typeof opts.write === "boolean" ? DEFAULT_TOKENSDIR : opts.write;
25
29
  const props = {
26
30
  themeName: theme,
27
31
  colors: {
@@ -33,10 +37,11 @@ function makeTokenCommands() {
33
37
  fontFamily
34
38
  }
35
39
  };
36
- const tokens = createTokens(props);
37
- if (write) {
38
- await writeTokens({ writeDir: write, tokens, themeName: theme, colors: props.colors });
40
+ if (dry) {
41
+ console.log(`Performing dry run, no files will be written`);
39
42
  }
43
+ const tokens = createTokens(props);
44
+ await writeTokens({ outDir, tokens, themeName: theme, colors: props.colors, dry });
40
45
  return Promise.resolve();
41
46
  });
42
47
  return tokenCmd;
@@ -0,0 +1,54 @@
1
+ export declare const luminance: {
2
+ light: {
3
+ backgroundDefault: number;
4
+ backgroundSubtle: number;
5
+ surfaceDefault: number;
6
+ surfaceHover: number;
7
+ surfaceActive: number;
8
+ borderSubtle: number;
9
+ borderDefault: number;
10
+ borderStrong: number;
11
+ baseDefault: number;
12
+ baseHover: number;
13
+ baseActive: number;
14
+ textSubtle: number;
15
+ textDefault: number;
16
+ baseContrastDefault: number;
17
+ baseContrastSubtle: number;
18
+ };
19
+ dark: {
20
+ backgroundDefault: number;
21
+ backgroundSubtle: number;
22
+ surfaceDefault: number;
23
+ surfaceHover: number;
24
+ surfaceActive: number;
25
+ borderSubtle: number;
26
+ borderDefault: number;
27
+ borderStrong: number;
28
+ baseDefault: number;
29
+ baseHover: number;
30
+ baseActive: number;
31
+ textSubtle: number;
32
+ textDefault: number;
33
+ baseContrastDefault: number;
34
+ baseContrastSubtle: number;
35
+ };
36
+ contrast: {
37
+ backgroundDefault: number;
38
+ backgroundSubtle: number;
39
+ surfaceDefault: number;
40
+ surfaceHover: number;
41
+ surfaceActive: number;
42
+ borderSubtle: number;
43
+ borderDefault: number;
44
+ borderStrong: number;
45
+ baseDefault: number;
46
+ baseHover: number;
47
+ baseActive: number;
48
+ textSubtle: number;
49
+ textDefault: number;
50
+ baseContrastDefault: number;
51
+ baseContrastSubtle: number;
52
+ };
53
+ };
54
+ //# sourceMappingURL=luminance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"luminance.d.ts","sourceRoot":"","sources":["../../../src/colors/luminance.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoDrB,CAAC"}
@@ -0,0 +1,56 @@
1
+ const luminance = {
2
+ light: {
3
+ backgroundDefault: 1,
4
+ backgroundSubtle: 0.9,
5
+ surfaceDefault: 0.76,
6
+ surfaceHover: 0.64,
7
+ surfaceActive: 0.54,
8
+ borderSubtle: 0.5,
9
+ borderDefault: 0.21,
10
+ borderStrong: 0.12,
11
+ baseDefault: 1,
12
+ baseHover: 1,
13
+ baseActive: 1,
14
+ textSubtle: 0.12,
15
+ textDefault: 0.0245,
16
+ baseContrastDefault: 1,
17
+ baseContrastSubtle: 1
18
+ },
19
+ dark: {
20
+ backgroundDefault: 0.012,
21
+ backgroundSubtle: 0.018,
22
+ surfaceDefault: 0.029,
23
+ surfaceHover: 0.046,
24
+ surfaceActive: 0.069,
25
+ borderSubtle: 0.082,
26
+ borderDefault: 0.2,
27
+ borderStrong: 0.36,
28
+ baseDefault: 1,
29
+ baseHover: 1,
30
+ baseActive: 1,
31
+ textSubtle: 0.36,
32
+ textDefault: 0.78,
33
+ baseContrastDefault: 1,
34
+ baseContrastSubtle: 1
35
+ },
36
+ contrast: {
37
+ backgroundDefault: 1e-3,
38
+ backgroundSubtle: 65e-4,
39
+ surfaceDefault: 0.015,
40
+ surfaceHover: 0.028,
41
+ surfaceActive: 0.044,
42
+ borderSubtle: 0.26,
43
+ borderDefault: 0.4,
44
+ borderStrong: 0.6,
45
+ baseDefault: 1,
46
+ baseHover: 1,
47
+ baseActive: 1,
48
+ textSubtle: 0.57,
49
+ textDefault: 0.86,
50
+ baseContrastDefault: 1,
51
+ baseContrastSubtle: 1
52
+ }
53
+ };
54
+ export {
55
+ luminance
56
+ };
@@ -1,90 +1,36 @@
1
- import type { CssColor } from '@adobe/leonardo-contrast-colors';
2
- import type { Colors } from '../tokens/types.js';
3
- import type { ColorInfo, ColorMode, ColorNumber, ContrastMode, GlobalColors, ThemeInfo } from './types.js';
1
+ import type { CssColor } from './types.js';
2
+ import type { ColorInfo, ColorNumber, ColorScheme, GlobalColors, ThemeInfo } from './types.js';
4
3
  export declare const baseColors: Record<GlobalColors, CssColor>;
5
- export type ColorError = 'none' | 'decorative' | 'interaction';
6
- type GlobalGenType = {
7
- themeMode?: ColorMode | 'all';
8
- contrastMode?: ContrastMode;
9
- };
10
- type ThemeGenType = {
11
- colors: Colors;
12
- contrastMode?: ContrastMode;
13
- };
14
4
  /**
15
- *
16
- * Generates a color scale based on a base color and a mode.
5
+ * Generates a color scale based on a base color and a color mode.
17
6
  *
18
7
  * @param color The base color that is used to generate the color scale
19
8
  * @param colorScheme The color scheme to generate a scale for
20
9
  */
21
- export declare const generateScaleForColor: (color: CssColor, colorScheme: ColorMode, contrastMode?: "aa" | "aaa") => ColorInfo[];
22
- /**
23
- *
24
- * Generates a color theme based on a base color. Light, Dark and Contrast scales are includes.
25
- *
26
- * @param color The base color that is used to generate the color theme
27
- */
28
- export declare const generateThemeForColor: (color: CssColor, contrastMode?: "aa" | "aaa") => ThemeInfo;
29
- export declare const generateGlobalColors: ({ contrastMode }: GlobalGenType) => Record<GlobalColors, ThemeInfo>;
30
- type GeneratedColorTheme = {
31
- main: Record<string, ThemeInfo>;
32
- support: Record<string, ThemeInfo>;
33
- neutral: ThemeInfo;
34
- };
10
+ export declare const generateColorScale: (color: CssColor, colorScheme: ColorScheme) => ColorInfo[];
35
11
  /**
36
- * This function generates a complete theme for a set of colors.
12
+ * Generates color schemes based on a base color. Light, Dark and Contrast scales are included.
37
13
  *
38
- * @param colors Which colors to generate the theme for
39
- * @param contrastMode The contrast mode to use
40
- * @returns
14
+ * @param color The base color that is used to generate the color schemes
41
15
  */
42
- export declare const generateColorTheme: ({ colors, contrastMode }: ThemeGenType) => GeneratedColorTheme;
16
+ export declare const generateColorSchemes: (color: CssColor) => ThemeInfo;
43
17
  /**
44
- *
45
- * This function calculates a color that can be used as a strong contrast color to a base color.
18
+ * Creates the Base Contrast Default color
46
19
  *
47
20
  * @param baseColor The base color
48
21
  */
49
- export declare const calculateContrastOneColor: (baseColor: CssColor) => "#000000" | "#ffffff";
22
+ export declare const getContrastDefault: (color: CssColor) => CssColor;
50
23
  /**
51
- *
52
- * This function calculates a color that can be used as a subtle contrast color to a base color.
24
+ * Creates the Base Contrast Subtle color
53
25
  *
54
26
  * @param color The base color
55
27
  */
56
- export declare const calculateContrastTwoColor: (color: CssColor) => CssColor;
57
- /**
58
- *
59
- * This function checks if white or black text can be used on 2 different colors at 4.5:1 contrast.
60
- *
61
- * @param baseDefaultColor Base default color
62
- * @param baseActiveColor Base active color
63
- */
64
- export declare const canTextBeUsedOnColors: (baseDefaultColor: CssColor, baseActiveColor: CssColor) => boolean;
28
+ export declare const getContrastSubtle: (color: CssColor) => CssColor;
65
29
  /**
30
+ * Returns the css variable for a color.
66
31
  *
67
- * This function creates a color with a specific lightness value.
68
- *
69
- * @param color The base color
70
- * @param lightness The lightness value from 0 to 100
71
- */
72
- export declare const createColorWithLightness: (color: CssColor, lightness: number) => CssColor;
73
- /**
74
- *
75
- * This function returns the color number based on the color name.
76
- *
77
- * @param name The name of the color
78
- */
79
- export declare const getColorNumberFromName: (name: string) => ColorNumber;
80
- /**
81
- *
82
- * This function returns the color name based on the color number.
83
- *
84
- * @param number The number of the color
32
+ * @param colorType The type of color
33
+ * @param colorNumber The number of the color
85
34
  */
86
- export declare const getColorNameFromNumber: (number: ColorNumber) => string;
87
- export declare const getBaseColor: (color: CssColor) => CssColor;
88
35
  export declare const getCssVariable: (colorType: string, colorNumber: ColorNumber) => string;
89
- export {};
90
36
  //# sourceMappingURL=theme.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../../../src/colors/theme.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ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, Theme } from "@adobe/leonardo-contrast-colors";
2
- import { Hsluv } from "hsluv";
3
- import * as R from "ramda";
4
- import { getContrastFromHex, getContrastFromLightness, getLightnessFromHex } from "./utils.js";
1
+ import chroma from "chroma-js";
2
+ import { luminance } from "./luminance.js";
3
+ import { getColorNameFromNumber, getLightnessFromHex, getLuminanceFromLightness } from "./utils.js";
5
4
  const baseColors = {
6
5
  blue: "#0A71C0",
7
6
  green: "#068718",
@@ -10,238 +9,65 @@ const baseColors = {
10
9
  red: "#C01B1B",
11
10
  yellow: "#D4B12F"
12
11
  };
13
- const generateThemeColor = (color, colorScheme, contrastMode = "aa") => {
14
- const leoBackgroundColor = new BackgroundColor({
15
- name: "backgroundColor",
16
- colorKeys: ["#ffffff"],
17
- ratios: [1]
18
- });
19
- let colorLightness = getLightnessFromHex(color);
20
- if (colorScheme === "dark" || colorScheme === "contrast") {
21
- color = getBaseColor(color);
22
- colorLightness = colorLightness <= 30 ? 70 : 100 - colorLightness;
23
- }
24
- let modifier = 8;
25
- if (colorLightness <= 30) {
26
- modifier = -modifier;
27
- }
28
- if (colorLightness >= 49 && colorLightness <= 65) {
29
- modifier = -modifier;
30
- }
31
- const baseDefaultContrast = getContrastFromLightness(colorLightness, color, leoBackgroundColor.colorKeys[0]);
32
- const baseHoverContrast = getContrastFromLightness(colorLightness - modifier, color, leoBackgroundColor.colorKeys[0]);
33
- const baseActiveContrast = getContrastFromLightness(
34
- colorLightness - modifier * 2,
35
- color,
36
- leoBackgroundColor.colorKeys[0]
37
- );
38
- const textSubLightLightness = contrastMode === "aa" ? 41 : 30;
39
- const textDefLightLightness = contrastMode === "aa" ? 18 : 12;
40
- const textSubDarkLightness = contrastMode === "aa" ? 66 : 80;
41
- const textDefDarkLightness = contrastMode === "aa" ? 90 : 94;
42
- let lightnessScale = [];
43
- if (colorScheme === "light") {
44
- lightnessScale = [100, 96, 90, 84, 78, 76, 53, 41, textSubLightLightness, textDefLightLightness];
45
- } else if (colorScheme === "dark") {
46
- lightnessScale = [10, 14, 20, 26, 32, 35, 52, 66, textSubDarkLightness, textDefDarkLightness];
47
- } else {
48
- lightnessScale = [1, 6, 14, 20, 26, 58, 70, 82, 80, 95];
49
- }
50
- const getColorContrasts = (color2, lightnessScale2, backgroundColor) => {
51
- return lightnessScale2.map((lightness) => getContrastFromLightness(lightness, color2, backgroundColor));
52
- };
53
- return new Color({
54
- name: "color",
55
- colorKeys: [color],
56
- ratios: [
57
- ...getColorContrasts(color, lightnessScale.slice(0, 8), leoBackgroundColor.colorKeys[0]),
58
- baseDefaultContrast,
59
- baseHoverContrast,
60
- baseActiveContrast,
61
- ...getColorContrasts(color, lightnessScale.slice(8), leoBackgroundColor.colorKeys[0])
62
- ]
63
- });
64
- };
65
- const generateScaleForColor = (color, colorScheme, contrastMode = "aa") => {
66
- const themeColor = generateThemeColor(color, colorScheme, contrastMode);
67
- const leoBackgroundColor = new BackgroundColor({
68
- name: "backgroundColor",
69
- colorKeys: ["#ffffff"],
70
- ratios: [1]
71
- });
72
- const theme = new Theme({
73
- colors: [themeColor],
74
- backgroundColor: leoBackgroundColor,
75
- lightness: 100
76
- });
77
- const outputArray = [];
78
- for (let i = 0; i < theme.contrastColorValues.length; i++) {
79
- outputArray.push({
80
- hex: theme.contrastColorValues[i],
81
- number: i + 1,
82
- name: getColorNameFromNumber(i + 1)
83
- });
84
- }
85
- outputArray.push({
86
- hex: calculateContrastOneColor(theme.contrastColorValues[8]),
87
- number: 14,
88
- name: getColorNameFromNumber(14)
89
- });
90
- outputArray.push({
91
- hex: calculateContrastTwoColor(theme.contrastColorValues[8]),
92
- number: 15,
93
- name: getColorNameFromNumber(15)
94
- });
95
- if (colorScheme === "light") {
96
- outputArray[8].hex = color;
12
+ const generateColorScale = (color, colorScheme) => {
13
+ const baseColors2 = getBaseColors(color, colorScheme);
14
+ const luminanceValues = luminance[colorScheme];
15
+ const outputArray = Object.entries(luminanceValues).map(([key, value], index) => ({
16
+ name: key,
17
+ hex: chroma(color).luminance(value).hex(),
18
+ number: index + 1
19
+ }));
20
+ const specialColors = [
21
+ { hex: baseColors2.baseDefault, number: 9 },
22
+ { hex: baseColors2.baseHover, number: 10 },
23
+ { hex: baseColors2.baseActive, number: 11 },
24
+ { hex: getContrastDefault(baseColors2.baseDefault), number: 14 },
25
+ { hex: getContrastSubtle(baseColors2.baseDefault), number: 15 }
26
+ ];
27
+ for (const { hex, number } of specialColors) {
28
+ outputArray[number - 1] = {
29
+ hex,
30
+ number,
31
+ name: getColorNameFromNumber(number)
32
+ };
97
33
  }
98
34
  return outputArray;
99
35
  };
100
- const generateThemeForColor = (color, contrastMode = "aa") => {
101
- const lightScale = generateScaleForColor(color, "light", contrastMode);
102
- const darkScale = generateScaleForColor(color, "dark", contrastMode);
103
- const contrastScale = generateScaleForColor(color, "contrast", contrastMode);
104
- return {
105
- light: lightScale,
106
- dark: darkScale,
107
- contrast: contrastScale
108
- };
109
- };
110
- const generateGlobalColors = ({ contrastMode = "aa" }) => {
111
- const blueTheme = generateThemeForColor(baseColors.blue, contrastMode);
112
- const greenTheme = generateThemeForColor(baseColors.green, contrastMode);
113
- const orangeTheme = generateThemeForColor(baseColors.orange, contrastMode);
114
- const purpleTheme = generateThemeForColor(baseColors.purple, contrastMode);
115
- const redTheme = generateThemeForColor(baseColors.red, contrastMode);
116
- const yellowTheme = generateThemeForColor(baseColors.yellow, contrastMode);
117
- return {
118
- blue: blueTheme,
119
- green: greenTheme,
120
- orange: orangeTheme,
121
- purple: purpleTheme,
122
- red: redTheme,
123
- yellow: yellowTheme
124
- };
125
- };
126
- const generateColorTheme = ({ colors, contrastMode = "aa" }) => {
127
- const main = R.map((color) => generateThemeForColor(color, contrastMode), colors.main);
128
- const support = R.map((color) => generateThemeForColor(color, contrastMode), colors.support);
129
- const neutral = generateThemeForColor(colors.neutral, contrastMode);
36
+ const generateColorSchemes = (color) => ({
37
+ light: generateColorScale(color, "light"),
38
+ dark: generateColorScale(color, "dark"),
39
+ contrast: generateColorScale(color, "contrast")
40
+ });
41
+ const getBaseColors = (color, colorScheme) => {
42
+ let colorLightness = getLightnessFromHex(color);
43
+ if (colorScheme !== "light") {
44
+ colorLightness = colorLightness <= 30 ? 70 : 100 - colorLightness;
45
+ }
46
+ const modifier = colorLightness <= 30 || colorLightness >= 49 && colorLightness <= 65 ? -8 : 8;
47
+ const calculateLightness = (base, mod) => base - mod;
130
48
  return {
131
- main,
132
- support,
133
- neutral
49
+ baseDefault: chroma(color).luminance(getLuminanceFromLightness(colorLightness)).hex(),
50
+ baseHover: chroma(color).luminance(getLuminanceFromLightness(calculateLightness(colorLightness, modifier))).hex(),
51
+ baseActive: chroma(color).luminance(getLuminanceFromLightness(calculateLightness(colorLightness, modifier * 2))).hex()
134
52
  };
135
53
  };
136
- const calculateContrastOneColor = (baseColor) => {
137
- const contrastWhite = getContrastFromHex(baseColor, "#ffffff");
138
- const contrastBlack = getContrastFromHex(baseColor, "#000000");
139
- const lightness = contrastWhite >= contrastBlack ? 100 : 0;
140
- return lightness === 0 ? "#000000" : "#ffffff";
141
- };
142
- const calculateContrastTwoColor = (color) => {
143
- const contrastWhite = getContrastFromHex(color, "#ffffff");
144
- const contrastBlack = getContrastFromHex(color, "#000000");
54
+ const getContrastDefault = (color) => chroma.contrast(color, "#ffffff") >= chroma.contrast(color, "#000000") ? "#ffffff" : "#000000";
55
+ const getContrastSubtle = (color) => {
56
+ const contrastWhite = chroma.contrast(color, "#ffffff");
57
+ const contrastBlack = chroma.contrast(color, "#000000");
145
58
  const lightness = getLightnessFromHex(color);
146
- const doubleALightnessModifier = lightness <= 40 ? 60 : lightness >= 60 ? 60 : 50;
147
- let targetLightness = 0;
148
- const contrastDirection = contrastWhite >= contrastBlack ? "lighten" : "darken";
149
- targetLightness = contrastDirection === "lighten" ? lightness + doubleALightnessModifier : lightness - doubleALightnessModifier;
150
- return createColorWithLightness(color, targetLightness);
151
- };
152
- const canTextBeUsedOnColors = (baseDefaultColor, baseActiveColor) => {
153
- const defaultAgainstWhite = getContrastFromHex(baseDefaultColor, "#ffffff");
154
- const defaultAgainstBlack = getContrastFromHex(baseDefaultColor, "#000000");
155
- const activeAgainstWhite = getContrastFromHex(baseActiveColor, "#ffffff");
156
- const activeAgainstBlack = getContrastFromHex(baseActiveColor, "#000000");
157
- if (defaultAgainstWhite >= 4.5 && activeAgainstWhite >= 4.5) {
158
- return true;
159
- }
160
- if (defaultAgainstBlack >= 4.5 && activeAgainstBlack >= 4.5) {
161
- return true;
162
- }
163
- return false;
164
- };
165
- const createColorWithLightness = (color, lightness) => {
166
- const leoBackgroundColor = new BackgroundColor({
167
- name: "backgroundColor",
168
- colorKeys: ["#ffffff"],
169
- ratios: [1]
170
- });
171
- const colors = new Color({
172
- name: "color",
173
- colorKeys: [color],
174
- ratios: [getContrastFromLightness(lightness, color, "#ffffff")]
175
- });
176
- const theme = new Theme({
177
- colors: [colors],
178
- backgroundColor: leoBackgroundColor,
179
- lightness: 100
180
- });
181
- return theme.contrastColorValues[0];
182
- };
183
- const getColorNumberFromName = (name) => {
184
- const colorMap = {
185
- "Background Default": 1,
186
- "Background Subtle": 2,
187
- "Surface Default": 3,
188
- "Surface Hover": 4,
189
- "Surface Active": 5,
190
- "Border Subtle": 6,
191
- "Border Default": 7,
192
- "Border Strong": 8,
193
- "Base Default": 9,
194
- "Base Hover": 10,
195
- "Base Active": 11,
196
- "Text Subtle": 12,
197
- "Text Default": 13,
198
- "Contrast Default": 14,
199
- "Contrast Subtle": 15
200
- };
201
- return colorMap[name];
202
- };
203
- const getColorNameFromNumber = (number) => {
204
- const colorMap = {
205
- 1: "Background Default",
206
- 2: "Background Subtle",
207
- 3: "Surface Default",
208
- 4: "Surface Hover",
209
- 5: "Surface Active",
210
- 6: "Border Subtle",
211
- 7: "Border Default",
212
- 8: "Border Strong",
213
- 9: "Base Default",
214
- 10: "Base Hover",
215
- 11: "Base Active",
216
- 12: "Text Subtle",
217
- 13: "Text Default",
218
- 14: "Contrast Default",
219
- 15: "Contrast Subtle"
220
- };
221
- return colorMap[number];
222
- };
223
- const getBaseColor = (color) => {
224
- const conv = new Hsluv();
225
- conv.hex = color;
226
- conv.hexToHsluv();
227
- conv.hsluvToHex();
228
- return conv.hex;
59
+ const modifier = lightness <= 40 || lightness >= 60 ? 60 : 50;
60
+ const targetLightness = contrastWhite >= contrastBlack ? lightness + modifier : lightness - modifier;
61
+ return chroma(color).luminance(getLuminanceFromLightness(targetLightness)).hex();
229
62
  };
230
63
  const getCssVariable = (colorType, colorNumber) => {
231
64
  return `--ds-color-${colorType}-${getColorNameFromNumber(colorNumber).toLowerCase().replace(/\s/g, "-")}`;
232
65
  };
233
66
  export {
234
67
  baseColors,
235
- calculateContrastOneColor,
236
- calculateContrastTwoColor,
237
- canTextBeUsedOnColors,
238
- createColorWithLightness,
239
- generateColorTheme,
240
- generateGlobalColors,
241
- generateScaleForColor,
242
- generateThemeForColor,
243
- getBaseColor,
244
- getColorNameFromNumber,
245
- getColorNumberFromName,
68
+ generateColorScale,
69
+ generateColorSchemes,
70
+ getContrastDefault,
71
+ getContrastSubtle,
246
72
  getCssVariable
247
73
  };
@@ -1,8 +1,8 @@
1
- import type { CssColor } from '@adobe/leonardo-contrast-colors';
2
- export type ColorMode = 'light' | 'dark' | 'contrast';
1
+ export type ColorScheme = 'light' | 'dark' | 'contrast';
3
2
  export type ContrastMode = 'aa' | 'aaa';
4
3
  export type ColorNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15;
5
4
  export type GlobalColors = 'red' | 'blue' | 'green' | 'orange' | 'purple' | 'yellow';
5
+ export type ColorError = 'none' | 'decorative' | 'interaction';
6
6
  export type ColorInfo = {
7
7
  hex: CssColor;
8
8
  number: ColorNumber;
@@ -13,4 +13,25 @@ export type ThemeInfo = {
13
13
  dark: ColorInfo[];
14
14
  contrast: ColorInfo[];
15
15
  };
16
+ /**
17
+ * A valid CSS color.
18
+ */
19
+ export type CssColor = RgbHexColor | RgbColor | HslColor | HsvColor | HsluvColor | LabColor | LchColor | OkLabColor | OkLchColor | Cam02Color | Cam02pColor;
20
+ /**
21
+ * Different color formats.
22
+ */
23
+ type RgbHexColor = `#${string}`;
24
+ type RgbColor = `rgb(${number} ${number} ${number})`;
25
+ type HslColor = `hsl(${Degrees} ${Percent} ${Percent})`;
26
+ type HsvColor = `hsv(${Degrees} ${Percent} ${Percent})`;
27
+ type HsluvColor = `hsluv(${number} ${number} ${number})`;
28
+ type LabColor = `lab(${Percent} ${number} ${number})`;
29
+ type LchColor = `lch(${Percent} ${number} ${Degrees})`;
30
+ type OkLabColor = `oklab(${Percent} ${number} ${number})`;
31
+ type OkLchColor = `oklch(${Percent} ${number} ${Degrees})`;
32
+ type Cam02Color = `jab(${Percent} ${number} ${number})`;
33
+ type Cam02pColor = `jch(${Percent} ${number} ${Degrees})`;
34
+ type Percent = `${number}%`;
35
+ type Degrees = `${number}deg`;
36
+ export {};
16
37
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/colors/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAEhE,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,UAAU,CAAC;AACtD,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,KAAK,CAAC;AACxC,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;AAC1F,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AACrF,MAAM,MAAM,SAAS,GAAG;IACtB,GAAG,EAAE,QAAQ,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