pacem-less 0.51.4-babbage → 0.51.4-boole

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 (2) hide show
  1. package/package.json +1 -1
  2. package/themebuilder.js +24 -15
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.51.4-babbage",
2
+ "version": "0.51.4-boole",
3
3
  "name": "pacem-less",
4
4
  "homepage": "https://js.pacem.it",
5
5
  "repository": {
package/themebuilder.js CHANGED
@@ -30,6 +30,10 @@ const argv = yargs
30
30
  .option('void', {
31
31
  type: 'string',
32
32
  description: 'Creates an empty palette file.',
33
+ })
34
+ .option('mincontrast', {
35
+ type: 'number',
36
+ description: 'The minimum WCAG contrast (1 to 21) container-to-background (default 1.5).',
33
37
  })
34
38
  .help()
35
39
  .argv;
@@ -55,7 +59,7 @@ function readFile(src) {
55
59
  const json = fs.readFileSync(src, 'utf-8');
56
60
  return JSON.parse(json);
57
61
  }
58
- const palette = readFile(argv.src) ?? {
62
+ const palette = Object.assign({
59
63
  background: '#1e2336',
60
64
  primary: '#429bbb',
61
65
  secondary: '#5e7881',
@@ -75,24 +79,30 @@ const palette = readFile(argv.src) ?? {
75
79
  purple: '#624472',
76
80
  teal: '#429bbb',
77
81
  gray: '#808080',
78
- };
79
- const WCAG_MINIMUM_CONTRAST = 3;
82
+ }, readFile(argv.src) ?? {});
83
+ const WCAG_MINIMUM_CONTRAST = argv.mincontrast ?? 1.5;
80
84
  const WCAG_NORMAL_TEXT_CONTRAST = 4.5;
81
85
  const WCAG_SAFE_CONTRAST = 7;
82
- const WCAG_MAX_CONTRAST = 17;
86
+ const WCAG_MAX_CONTRAST = 15;
87
+ const WCAG_DISABLED_CONTRAST = 1.8;
83
88
  const SURFACE_LIGHT = .95;
84
89
  const SURFACE_DARK = .02;
85
90
  const MIN_SATURATION = .175;
86
91
  const searchFn = Pacem.Mathematics.DataAnalysis.SearchFunctions.goldenRatio;
92
+ let backgroundModule;
93
+ let rootModule;
87
94
  function isColorLight(clr) {
88
95
  return Pacem.Colors.luminance(clr) >= .2;
89
96
  }
90
- function contrastColor(baseColor, targetContrast = WCAG_NORMAL_TEXT_CONTRAST) {
97
+ function contrastColor(baseColor, arg1 = baseColor, arg2 = WCAG_NORMAL_TEXT_CONTRAST, arg3 = null) {
98
+ const benchmarkColor = typeof arg1 === 'object' ? arg1 : baseColor;
99
+ const targetContrast = typeof arg1 === 'number' ? arg1 : (typeof arg2 === 'number' ? arg2 : WCAG_NORMAL_TEXT_CONTRAST);
100
+ const preferDark = typeof arg2 === 'boolean' ? arg2 : arg3;
91
101
  const { h, s, l, a } = Pacem.Colors.hsl(baseColor);
92
- const isDark = !isColorLight(baseColor);
102
+ const isDark = preferDark ?? !isColorLight(baseColor);
93
103
  const min = isDark ? l : 0;
94
104
  const max = isDark ? 1 : l;
95
- const newL = searchFn(min, max, x => Math.abs(targetContrast - Pacem.Colors.contrastRatio(baseColor, { h, s, l: x, a })));
105
+ const newL = searchFn(min, max, x => Math.abs(targetContrast - Pacem.Colors.contrastRatio(benchmarkColor, { h, s, l: x, a })));
96
106
  return Pacem.Colors.rgb({ h, s, l: newL, a });
97
107
  }
98
108
  function luminanceColor(baseColor, targetLuminance) {
@@ -102,9 +112,9 @@ function luminanceColor(baseColor, targetLuminance) {
102
112
  }
103
113
  function colorModule(clr, name, arg2) {
104
114
  const isDisabled = name === 'disabled';
105
- const isSurfaceColor = name === 'root' || name === 'background' || isDisabled;
115
+ const isSurfaceColor = name === 'root' || name === 'background';
106
116
  const isDesaturated = isDisabled || name === 'gray';
107
- const forcedContrast = (arg2 && typeof arg2 === 'number') ? arg2 : (isDisabled ? 1.7 : null);
117
+ const forcedContrast = (arg2 && typeof arg2 === 'number') ? arg2 : (isDisabled ? WCAG_DISABLED_CONTRAST : null);
108
118
  const baseContrast = forcedContrast ?? (isSurfaceColor ? WCAG_MAX_CONTRAST : WCAG_SAFE_CONTRAST);
109
119
  const contrastClr = arg2 && typeof arg2 === 'object' ? arg2 : null;
110
120
  const surfaceLight = name === 'background' ? SURFACE_LIGHT + .015 : SURFACE_LIGHT;
@@ -114,7 +124,7 @@ function colorModule(clr, name, arg2) {
114
124
  const lightTargetLuminance = isDisabled ? (surfaceLight - .1) : (isSurfaceColor ? surfaceLight : .333);
115
125
  const referenceColor = isColorRight ? clr
116
126
  : (isSurfaceColor ? (!darkTheme ? luminanceColor(clr, lightTargetLuminance) : luminanceColor(clr, darkTargetLuminance))
117
- : (darkTheme ? luminanceColor(clr, lightTargetLuminance) : luminanceColor(clr, darkTargetLuminance)));
127
+ : contrastColor(clr, backgroundModule.base, WCAG_NORMAL_TEXT_CONTRAST, darkTheme));
118
128
  let actualColor = contrastClr ? clr : referenceColor;
119
129
  if (isDesaturated) {
120
130
  const { h, s, l, a } = Pacem.Colors.hsl(actualColor);
@@ -126,7 +136,7 @@ function colorModule(clr, name, arg2) {
126
136
  const luminanceBackActiveOffset = darkTheme ? (isSurfaceColor ? .035 : .1) : (isSurfaceColor ? -.08 : -.06);
127
137
  const contrastForeActive = forcedContrast ?? WCAG_SAFE_CONTRAST;
128
138
  const contrast = contrastClr ?? contrastColor(actualColor, baseContrast);
129
- const container = luminanceColor(actualColor, luminanceBackContainer);
139
+ const container = isSurfaceColor ? luminanceColor(actualColor, luminanceBackContainer) : contrastColor(actualColor, backgroundModule.base, WCAG_MINIMUM_CONTRAST);
130
140
  const containerContrast = contrastColor(container, isDisabled ? forcedContrast : WCAG_NORMAL_TEXT_CONTRAST);
131
141
  const active = isDisabled ? actualColor : luminanceColor(actualColor, Pacem.Colors.luminance(actualColor) + luminanceBackActiveOffset);
132
142
  const activeContrast = isDisabled ? contrast : contrastColor(active, contrastForeActive);
@@ -146,8 +156,6 @@ function colorModule(clr, name, arg2) {
146
156
  };
147
157
  }
148
158
  const lessVariables = {};
149
- let backgroundModule;
150
- let rootModule;
151
159
  const a255 = (x) => Math.round(x * 255);
152
160
  const a255s = (c) => `${a255(c.r)}, ${a255(c.g)}, ${a255(c.b)}`;
153
161
  const pushLessVariables = (name, mod) => {
@@ -187,8 +195,9 @@ if (!('root' in palette)) {
187
195
  pushLessVariables('root', rootModule);
188
196
  }
189
197
  if (!('disabled' in palette)) {
190
- const disabledColor = darkTheme ? Pacem.Colors.lighten(backgroundModule.base, .04) : Pacem.Colors.darken(backgroundModule.base, .085);
191
- pushLessVariables('disabled', colorModule(disabledColor, 'disabled'));
198
+ const disabledColor = contrastColor(backgroundModule.base, WCAG_DISABLED_CONTRAST);
199
+ const disabledContrastColor = contrastColor(disabledColor, WCAG_DISABLED_CONTRAST * (darkTheme ? 1 : .9), !darkTheme);
200
+ pushLessVariables('disabled', colorModule(disabledColor, 'disabled', disabledContrastColor));
192
201
  }
193
202
  if (!('default' in palette)) {
194
203
  for (let name of ['default', 'invert']) {