@skbkontur/colors 2.0.4 → 2.0.6-alpha.0
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.
- package/default-dark.d.ts +35 -35
- package/default-dark.js +35 -35
- package/default-light.d.ts +34 -34
- package/default-light.js +34 -34
- package/get-colors.d.ts +1 -0
- package/get-colors.js +1 -0
- package/index.d.ts +319 -0
- package/index.js +319 -0
- package/lib/get-colors-base.js +5 -4
- package/lib/get-colors-base.ts +5 -4
- package/lib/helpers/get-on-brand.d.ts +28 -0
- package/lib/helpers/get-on-brand.js +10 -0
- package/lib/helpers/get-on-brand.ts +13 -0
- package/lib/helpers/get-palette.d.ts +3 -36
- package/lib/helpers/get-palette.js +65 -98
- package/lib/helpers/get-palette.ts +73 -117
- package/package.json +5 -5
- package/tokens/brand-blue-deep_accent-brand.css +69 -69
- package/tokens/brand-blue-deep_accent-gray.css +69 -69
- package/tokens/brand-blue_accent-brand.css +69 -69
- package/tokens/brand-blue_accent-gray.css +69 -69
- package/tokens/brand-green_accent-brand.css +69 -69
- package/tokens/brand-green_accent-gray.css +69 -69
- package/tokens/brand-mint_accent-brand.css +69 -69
- package/tokens/brand-mint_accent-gray.css +69 -69
- package/tokens/brand-orange_accent-gray.css +88 -88
- package/tokens/brand-purple_accent-brand.css +69 -69
- package/tokens/brand-purple_accent-gray.css +69 -69
- package/tokens/brand-red_accent-gray.css +69 -69
- package/tokens/brand-violet_accent-brand.css +69 -69
- package/tokens/brand-violet_accent-gray.css +69 -69
- package/tokens-mobile/brand-blue-deep_accent-brand.json +37 -37
- package/tokens-mobile/brand-blue-deep_accent-gray.json +37 -37
- package/tokens-mobile/brand-blue_accent-brand.json +37 -37
- package/tokens-mobile/brand-blue_accent-gray.json +37 -37
- package/tokens-mobile/brand-green_accent-brand.json +37 -37
- package/tokens-mobile/brand-green_accent-gray.json +37 -37
- package/tokens-mobile/brand-mint_accent-brand.json +37 -37
- package/tokens-mobile/brand-mint_accent-gray.json +37 -37
- package/tokens-mobile/brand-orange_accent-gray.json +45 -45
- package/tokens-mobile/brand-purple_accent-brand.json +37 -37
- package/tokens-mobile/brand-purple_accent-gray.json +37 -37
- package/tokens-mobile/brand-red_accent-gray.json +37 -37
- package/tokens-mobile/brand-violet_accent-brand.json +37 -37
- package/tokens-mobile/brand-violet_accent-gray.json +37 -37
|
@@ -19,14 +19,14 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
19
19
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
20
|
};
|
|
21
21
|
import { clampChroma, converter } from 'culori';
|
|
22
|
-
import { calcAPCA } from 'apca-w3';
|
|
23
|
-
import * as DefaultSwatch from '../consts/default-swatch.js';
|
|
24
22
|
import { CHROMA_PARAMS } from '../consts/params/chroma-params.js';
|
|
25
23
|
import { ABNEY_CORRECTION } from '../consts/params/abney-correction.js';
|
|
26
24
|
import { PROMO_HUE_SHIFTS } from '../consts/params/promo-hue-shift.js';
|
|
27
25
|
import { WARNING_HUE_PATCH } from '../consts/params/warning-hue-patch.js';
|
|
26
|
+
var toOklch = converter('oklch');
|
|
28
27
|
export function getPalette(_a) {
|
|
29
|
-
var
|
|
28
|
+
var _b;
|
|
29
|
+
var color = _a.color, _c = _a.type, type = _c === void 0 ? 'default' : _c, customSettings = _a.settings;
|
|
30
30
|
var defaultSettings = {
|
|
31
31
|
chromaSettings: CHROMA_PARAMS,
|
|
32
32
|
abneyCorrection: ABNEY_CORRECTION,
|
|
@@ -34,9 +34,6 @@ export function getPalette(_a) {
|
|
|
34
34
|
warningHuePatch: WARNING_HUE_PATCH,
|
|
35
35
|
};
|
|
36
36
|
var settings = __assign(__assign({}, defaultSettings), customSettings);
|
|
37
|
-
var toOklch = converter('oklch');
|
|
38
|
-
var oklchColor = toOklch(color);
|
|
39
|
-
var currentHue = (oklchColor === null || oklchColor === void 0 ? void 0 : oklchColor.h) || 0;
|
|
40
37
|
var toNorm = function (x) { return x / 100; };
|
|
41
38
|
var calculateChromaValue = function (rel, min, max, baseChromaMax) {
|
|
42
39
|
var raw = (baseChromaMax * rel) / 100;
|
|
@@ -55,6 +52,8 @@ export function getPalette(_a) {
|
|
|
55
52
|
dim: {},
|
|
56
53
|
};
|
|
57
54
|
var isWarning = type === 'warning';
|
|
55
|
+
var baseHue = calcBaseHue(color, settings.abneyCorrection);
|
|
56
|
+
var correctionHueRange = calcCorrectionRange(color, settings.abneyCorrection);
|
|
58
57
|
for (var Lstr in settings.chromaSettings) {
|
|
59
58
|
var L = +Lstr;
|
|
60
59
|
if (L === 100) {
|
|
@@ -63,52 +62,30 @@ export function getPalette(_a) {
|
|
|
63
62
|
result.dim[L] = "oklch(100% 0 0)";
|
|
64
63
|
continue;
|
|
65
64
|
}
|
|
66
|
-
var
|
|
67
|
-
var
|
|
68
|
-
|
|
65
|
+
var hue = baseHue;
|
|
66
|
+
var lightnessCorrectionData = settings.abneyCorrection[L];
|
|
67
|
+
if (lightnessCorrectionData && correctionHueRange !== undefined) {
|
|
68
|
+
var shift = (_b = lightnessCorrectionData[correctionHueRange]) !== null && _b !== void 0 ? _b : 0;
|
|
69
|
+
hue = (baseHue + shift + 360) % 360;
|
|
70
|
+
}
|
|
71
|
+
if (isWarning) {
|
|
72
|
+
hue = calcWarningHuePatch(hue, L, settings.warningHuePatch);
|
|
73
|
+
}
|
|
74
|
+
var chromaMax = clampChroma({ mode: 'oklch', l: L / 100, c: 1, h: hue }, 'oklch').c;
|
|
69
75
|
var currentParams = settings.chromaSettings[L];
|
|
70
76
|
var vividN = calculateChromaValue(currentParams.vivid.rel, currentParams.vivid.min, currentParams.vivid.max, chromaMax);
|
|
71
77
|
var normN = calculateChromaValue(currentParams.normal.rel, currentParams.normal.min, currentParams.normal.max, chromaMax);
|
|
72
78
|
var dimN = calculateChromaValue(currentParams.dim.rel, currentParams.dim.min, currentParams.dim.max, chromaMax);
|
|
73
|
-
result.vivid[L] = "oklch(".concat(L, "% ").concat(vividN.toFixed(3), " ").concat(
|
|
74
|
-
result.normal[L] = "oklch(".concat(L, "% ").concat(normN.toFixed(3), " ").concat(
|
|
75
|
-
result.dim[L] = "oklch(".concat(L, "% ").concat(dimN.toFixed(3), " ").concat(
|
|
79
|
+
result.vivid[L] = "oklch(".concat(L, "% ").concat(vividN.toFixed(3), " ").concat(hue.toFixed(0), ")");
|
|
80
|
+
result.normal[L] = "oklch(".concat(L, "% ").concat(normN.toFixed(3), " ").concat(hue.toFixed(0), ")");
|
|
81
|
+
result.dim[L] = "oklch(".concat(L, "% ").concat(dimN.toFixed(3), " ").concat(hue.toFixed(0), ")");
|
|
76
82
|
}
|
|
77
83
|
return result;
|
|
78
84
|
}
|
|
79
|
-
export function
|
|
80
|
-
var
|
|
81
|
-
if (
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
var hueRanges = Object.keys(lightnessData)
|
|
85
|
-
.map(Number)
|
|
86
|
-
.sort(function (a, b) { return a - b; });
|
|
87
|
-
var selectedHueRange = hueRanges[0];
|
|
88
|
-
for (var i = 0; i < hueRanges.length; i++) {
|
|
89
|
-
var startRange = hueRanges[i];
|
|
90
|
-
var endRange = hueRanges[i + 1] !== undefined ? hueRanges[i + 1] : 360;
|
|
91
|
-
if (currentHue >= startRange && currentHue < endRange) {
|
|
92
|
-
selectedHueRange = startRange;
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
if (i === hueRanges.length - 1 && currentHue >= startRange && currentHue < 360) {
|
|
96
|
-
selectedHueRange = startRange;
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return lightnessData[selectedHueRange] !== undefined ? lightnessData[selectedHueRange] : 0;
|
|
101
|
-
}
|
|
102
|
-
export function applyAbneyShift(lightness, currentHue, abneyData) {
|
|
103
|
-
var abneyShift = getAbneyHueShift(lightness, currentHue, abneyData);
|
|
104
|
-
return (currentHue + abneyShift + 360) % 360;
|
|
105
|
-
}
|
|
106
|
-
export function applyWarningHuePatch(currentHue, lightness, warningHuePatchData, isWarningMode) {
|
|
107
|
-
if (isWarningMode) {
|
|
108
|
-
var patch = warningHuePatchData[lightness];
|
|
109
|
-
if (patch !== undefined) {
|
|
110
|
-
return (currentHue + patch + 360) % 360;
|
|
111
|
-
}
|
|
85
|
+
export function calcWarningHuePatch(currentHue, lightness, warningHuePatchData) {
|
|
86
|
+
var patch = warningHuePatchData[lightness];
|
|
87
|
+
if (patch !== undefined) {
|
|
88
|
+
return (currentHue + patch + 360) % 360;
|
|
112
89
|
}
|
|
113
90
|
return currentHue;
|
|
114
91
|
}
|
|
@@ -129,30 +106,13 @@ function findClosestLightnessStep(targetL, availableLightnessSteps) {
|
|
|
129
106
|
}
|
|
130
107
|
return closestStep;
|
|
131
108
|
}
|
|
132
|
-
export function
|
|
133
|
-
var
|
|
134
|
-
var
|
|
135
|
-
|
|
136
|
-
console.warn("Could not parse color string: ".concat(inputColorString));
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
var targetLightness = Math.round(oklch.l * 100);
|
|
140
|
-
var targetHue = oklch.h;
|
|
141
|
-
if (isNaN(targetHue)) {
|
|
142
|
-
console.warn("Achromatic color detected (".concat(inputColorString, "). Defaulting hue to 0."));
|
|
143
|
-
targetHue = 0;
|
|
144
|
-
}
|
|
109
|
+
export function calcBaseHue(inputColorString, abneyData) {
|
|
110
|
+
var colorOKLCH = toOklch(inputColorString);
|
|
111
|
+
var targetLightness = colorOKLCH.l * 100;
|
|
112
|
+
var targetHue = colorOKLCH.h;
|
|
145
113
|
var availableLightnessSteps = Object.keys(abneyData).map(Number);
|
|
146
|
-
if (availableLightnessSteps.length === 0) {
|
|
147
|
-
console.warn('Abney correction data is empty, cannot calculate base hue.');
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
114
|
var closestLightness = findClosestLightnessStep(targetLightness, availableLightnessSteps);
|
|
151
115
|
var lightnessCorrectionData = abneyData[closestLightness];
|
|
152
|
-
if (!lightnessCorrectionData) {
|
|
153
|
-
console.warn("No Abney correction data for lightness ".concat(closestLightness, "."));
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
116
|
var correctedHueMap = Object.entries(lightnessCorrectionData).map(function (_a) {
|
|
157
117
|
var rawHueStr = _a[0], shift = _a[1];
|
|
158
118
|
var rawHue = Number(rawHueStr);
|
|
@@ -162,43 +122,50 @@ export function calculateBaseHueAndCorrectionRange(inputColorString, abneyData)
|
|
|
162
122
|
correctedHue: (rawHue + shift + 360) % 360,
|
|
163
123
|
};
|
|
164
124
|
});
|
|
165
|
-
correctedHueMap.sort(function (a, b) {
|
|
166
|
-
if (a.correctedHue !== b.correctedHue) {
|
|
167
|
-
return a.correctedHue - b.correctedHue;
|
|
168
|
-
}
|
|
169
|
-
return a.rawHue - b.rawHue;
|
|
170
|
-
});
|
|
125
|
+
correctedHueMap.sort(function (a, b) { return a.correctedHue - b.correctedHue; });
|
|
171
126
|
if (correctedHueMap.length === 0) {
|
|
172
|
-
|
|
173
|
-
return null;
|
|
127
|
+
return targetHue;
|
|
174
128
|
}
|
|
175
|
-
var
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
129
|
+
var selectedRange = correctedHueMap[correctedHueMap.length - 1];
|
|
130
|
+
for (var i = 0; i < correctedHueMap.length; i++) {
|
|
131
|
+
var current = correctedHueMap[i];
|
|
132
|
+
var next = correctedHueMap[i + 1];
|
|
133
|
+
if (!next) {
|
|
134
|
+
selectedRange = current;
|
|
135
|
+
break;
|
|
179
136
|
}
|
|
180
|
-
if (
|
|
181
|
-
|
|
137
|
+
if (targetHue >= current.correctedHue && targetHue < next.correctedHue) {
|
|
138
|
+
selectedRange = current;
|
|
139
|
+
break;
|
|
182
140
|
}
|
|
183
|
-
return correctedHueMap[correctedHueMap.length - 1];
|
|
184
|
-
};
|
|
185
|
-
var selectedRange = findCorrectRange();
|
|
186
|
-
if (!selectedRange) {
|
|
187
|
-
console.error('Could not determine the correct hue range.');
|
|
188
|
-
return null;
|
|
189
141
|
}
|
|
190
|
-
|
|
191
|
-
return {
|
|
192
|
-
baseHue: baseHue,
|
|
193
|
-
correctionLightness: closestLightness,
|
|
194
|
-
correctionHueRange: selectedRange.rawHue,
|
|
195
|
-
};
|
|
142
|
+
return (targetHue - selectedRange.shift + 360) % 360;
|
|
196
143
|
}
|
|
197
|
-
export function
|
|
198
|
-
var
|
|
199
|
-
var
|
|
200
|
-
|
|
201
|
-
|
|
144
|
+
export function calcCorrectionRange(inputColorString, abneyData) {
|
|
145
|
+
var colorOKLCH = toOklch(inputColorString);
|
|
146
|
+
var targetLightness = colorOKLCH.l * 100;
|
|
147
|
+
var targetHue = colorOKLCH.h;
|
|
148
|
+
var availableLightnessSteps = Object.keys(abneyData).map(Number);
|
|
149
|
+
var closestLightness = findClosestLightnessStep(targetLightness, availableLightnessSteps);
|
|
150
|
+
var lightnessCorrectionData = abneyData[closestLightness];
|
|
151
|
+
var correctedHueMap = Object.entries(lightnessCorrectionData).map(function (_a) {
|
|
152
|
+
var rawHueStr = _a[0], shift = _a[1];
|
|
153
|
+
var rawHue = Number(rawHueStr);
|
|
154
|
+
return {
|
|
155
|
+
rawHue: rawHue,
|
|
156
|
+
shift: shift,
|
|
157
|
+
correctedHue: (rawHue + shift + 360) % 360,
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
correctedHueMap.sort(function (a, b) { return a.correctedHue - b.correctedHue; });
|
|
161
|
+
var selectedRange = correctedHueMap[correctedHueMap.length - 1];
|
|
162
|
+
for (var i = 0; i < correctedHueMap.length; i++) {
|
|
163
|
+
var current = correctedHueMap[i];
|
|
164
|
+
var next = correctedHueMap[i + 1];
|
|
165
|
+
if (targetHue >= current.correctedHue && targetHue < next.correctedHue) {
|
|
166
|
+
selectedRange = current;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
202
169
|
}
|
|
203
|
-
return
|
|
170
|
+
return selectedRange.rawHue;
|
|
204
171
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { clampChroma, converter } from 'culori';
|
|
2
|
-
import { calcAPCA } from 'apca-w3';
|
|
3
2
|
|
|
4
3
|
import type {
|
|
5
4
|
GeneratorColorAbneyCorrection,
|
|
@@ -7,12 +6,13 @@ import type {
|
|
|
7
6
|
GeneratorColorPalette,
|
|
8
7
|
GeneratorColorWarningHuePatch,
|
|
9
8
|
} from '../types/tokens-base-generator.js';
|
|
10
|
-
import * as DefaultSwatch from '../consts/default-swatch.js';
|
|
11
9
|
import { CHROMA_PARAMS } from '../consts/params/chroma-params.js';
|
|
12
10
|
import { ABNEY_CORRECTION } from '../consts/params/abney-correction.js';
|
|
13
11
|
import { PROMO_HUE_SHIFTS } from '../consts/params/promo-hue-shift.js';
|
|
14
12
|
import { WARNING_HUE_PATCH } from '../consts/params/warning-hue-patch.js';
|
|
15
13
|
|
|
14
|
+
const toOklch = converter('oklch');
|
|
15
|
+
|
|
16
16
|
interface GeneratePaletteParams {
|
|
17
17
|
color: string;
|
|
18
18
|
type?: 'default' | 'warning';
|
|
@@ -41,10 +41,6 @@ export function getPalette({
|
|
|
41
41
|
...customSettings,
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const toOklch = converter('oklch');
|
|
45
|
-
const oklchColor = toOklch(color);
|
|
46
|
-
const currentHue = oklchColor?.h || 0;
|
|
47
|
-
|
|
48
44
|
const toNorm = (x: number) => x / 100;
|
|
49
45
|
|
|
50
46
|
const calculateChromaValue = (
|
|
@@ -70,6 +66,8 @@ export function getPalette({
|
|
|
70
66
|
dim: {},
|
|
71
67
|
};
|
|
72
68
|
const isWarning = type === 'warning';
|
|
69
|
+
const baseHue = calcBaseHue(color, settings.abneyCorrection);
|
|
70
|
+
const correctionHueRange = calcCorrectionRange(color, settings.abneyCorrection);
|
|
73
71
|
|
|
74
72
|
for (const Lstr in settings.chromaSettings) {
|
|
75
73
|
const L = +Lstr;
|
|
@@ -80,10 +78,18 @@ export function getPalette({
|
|
|
80
78
|
continue;
|
|
81
79
|
}
|
|
82
80
|
|
|
83
|
-
|
|
84
|
-
const
|
|
81
|
+
let hue = baseHue;
|
|
82
|
+
const lightnessCorrectionData = settings.abneyCorrection[L];
|
|
83
|
+
if (lightnessCorrectionData && correctionHueRange !== undefined) {
|
|
84
|
+
const shift = lightnessCorrectionData[correctionHueRange] ?? 0;
|
|
85
|
+
hue = (baseHue + shift + 360) % 360;
|
|
86
|
+
}
|
|
85
87
|
|
|
86
|
-
|
|
88
|
+
if (isWarning) {
|
|
89
|
+
hue = calcWarningHuePatch(hue, L, settings.warningHuePatch);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const chromaMax = clampChroma({ mode: 'oklch', l: L / 100, c: 1, h: hue }, 'oklch').c;
|
|
87
93
|
const currentParams = settings.chromaSettings[L];
|
|
88
94
|
|
|
89
95
|
const vividN = calculateChromaValue(
|
|
@@ -100,62 +106,22 @@ export function getPalette({
|
|
|
100
106
|
);
|
|
101
107
|
const dimN = calculateChromaValue(currentParams.dim.rel, currentParams.dim.min, currentParams.dim.max, chromaMax);
|
|
102
108
|
|
|
103
|
-
result.vivid[L] = `oklch(${L}% ${vividN.toFixed(3)} ${
|
|
104
|
-
result.normal[L] = `oklch(${L}% ${normN.toFixed(3)} ${
|
|
105
|
-
result.dim[L] = `oklch(${L}% ${dimN.toFixed(3)} ${
|
|
109
|
+
result.vivid[L] = `oklch(${L}% ${vividN.toFixed(3)} ${hue.toFixed(0)})`;
|
|
110
|
+
result.normal[L] = `oklch(${L}% ${normN.toFixed(3)} ${hue.toFixed(0)})`;
|
|
111
|
+
result.dim[L] = `oklch(${L}% ${dimN.toFixed(3)} ${hue.toFixed(0)})`;
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
return result;
|
|
109
115
|
}
|
|
110
116
|
|
|
111
|
-
export function
|
|
112
|
-
lightness: number,
|
|
113
|
-
currentHue: number,
|
|
114
|
-
abneyData: GeneratorColorAbneyCorrection
|
|
115
|
-
): number {
|
|
116
|
-
const lightnessData = abneyData[lightness];
|
|
117
|
-
if (!lightnessData) {
|
|
118
|
-
return 0;
|
|
119
|
-
}
|
|
120
|
-
const hueRanges = Object.keys(lightnessData)
|
|
121
|
-
.map(Number)
|
|
122
|
-
.sort((a, b) => a - b);
|
|
123
|
-
let selectedHueRange = hueRanges[0];
|
|
124
|
-
for (let i = 0; i < hueRanges.length; i++) {
|
|
125
|
-
const startRange = hueRanges[i];
|
|
126
|
-
const endRange = hueRanges[i + 1] !== undefined ? hueRanges[i + 1] : 360;
|
|
127
|
-
if (currentHue >= startRange && currentHue < endRange) {
|
|
128
|
-
selectedHueRange = startRange;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
if (i === hueRanges.length - 1 && currentHue >= startRange && currentHue < 360) {
|
|
132
|
-
selectedHueRange = startRange;
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return lightnessData[selectedHueRange] !== undefined ? lightnessData[selectedHueRange] : 0;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function applyAbneyShift(
|
|
140
|
-
lightness: number,
|
|
141
|
-
currentHue: number,
|
|
142
|
-
abneyData: GeneratorColorAbneyCorrection
|
|
143
|
-
): number {
|
|
144
|
-
const abneyShift = getAbneyHueShift(lightness, currentHue, abneyData);
|
|
145
|
-
return (currentHue + abneyShift + 360) % 360;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export function applyWarningHuePatch(
|
|
117
|
+
export function calcWarningHuePatch(
|
|
149
118
|
currentHue: number,
|
|
150
119
|
lightness: number,
|
|
151
|
-
warningHuePatchData: GeneratorColorWarningHuePatch
|
|
152
|
-
isWarningMode: boolean
|
|
120
|
+
warningHuePatchData: GeneratorColorWarningHuePatch
|
|
153
121
|
): number {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return (currentHue + patch + 360) % 360;
|
|
158
|
-
}
|
|
122
|
+
const patch = warningHuePatchData[lightness];
|
|
123
|
+
if (patch !== undefined) {
|
|
124
|
+
return (currentHue + patch + 360) % 360;
|
|
159
125
|
}
|
|
160
126
|
return currentHue;
|
|
161
127
|
}
|
|
@@ -178,37 +144,13 @@ function findClosestLightnessStep(targetL: number, availableLightnessSteps: numb
|
|
|
178
144
|
return closestStep;
|
|
179
145
|
}
|
|
180
146
|
|
|
181
|
-
export function
|
|
182
|
-
inputColorString:
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
baseHue: number;
|
|
186
|
-
correctionLightness: number;
|
|
187
|
-
correctionHueRange: number;
|
|
188
|
-
} | null {
|
|
189
|
-
const toOklch = converter('oklch');
|
|
190
|
-
const oklch = toOklch(inputColorString) as { l: number; c: number; h: number };
|
|
191
|
-
if (!oklch) {
|
|
192
|
-
console.warn(`Could not parse color string: ${inputColorString}`);
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
const targetLightness = Math.round(oklch.l * 100);
|
|
196
|
-
let targetHue = oklch.h;
|
|
197
|
-
if (isNaN(targetHue)) {
|
|
198
|
-
console.warn(`Achromatic color detected (${inputColorString}). Defaulting hue to 0.`);
|
|
199
|
-
targetHue = 0;
|
|
200
|
-
}
|
|
147
|
+
export function calcBaseHue(inputColorString: string, abneyData: GeneratorColorAbneyCorrection): number {
|
|
148
|
+
const colorOKLCH = toOklch(inputColorString) as { l: number; c: number; h: number };
|
|
149
|
+
const targetLightness = colorOKLCH.l * 100;
|
|
150
|
+
const targetHue = colorOKLCH.h;
|
|
201
151
|
const availableLightnessSteps = Object.keys(abneyData).map(Number);
|
|
202
|
-
if (availableLightnessSteps.length === 0) {
|
|
203
|
-
console.warn('Abney correction data is empty, cannot calculate base hue.');
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
152
|
const closestLightness = findClosestLightnessStep(targetLightness, availableLightnessSteps);
|
|
207
153
|
const lightnessCorrectionData = abneyData[closestLightness];
|
|
208
|
-
if (!lightnessCorrectionData) {
|
|
209
|
-
console.warn(`No Abney correction data for lightness ${closestLightness}.`);
|
|
210
|
-
return null;
|
|
211
|
-
}
|
|
212
154
|
const correctedHueMap = Object.entries(lightnessCorrectionData).map(([rawHueStr, shift]) => {
|
|
213
155
|
const rawHue = Number(rawHueStr);
|
|
214
156
|
return {
|
|
@@ -217,45 +159,59 @@ export function calculateBaseHueAndCorrectionRange(
|
|
|
217
159
|
correctedHue: (rawHue + shift + 360) % 360,
|
|
218
160
|
};
|
|
219
161
|
});
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return a.correctedHue - b.correctedHue;
|
|
223
|
-
}
|
|
224
|
-
return a.rawHue - b.rawHue;
|
|
225
|
-
});
|
|
162
|
+
|
|
163
|
+
correctedHueMap.sort((a, b) => a.correctedHue - b.correctedHue);
|
|
226
164
|
if (correctedHueMap.length === 0) {
|
|
227
|
-
|
|
228
|
-
return null;
|
|
165
|
+
return targetHue;
|
|
229
166
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
167
|
+
|
|
168
|
+
let selectedRange = correctedHueMap[correctedHueMap.length - 1];
|
|
169
|
+
for (let i = 0; i < correctedHueMap.length; i++) {
|
|
170
|
+
const current = correctedHueMap[i];
|
|
171
|
+
const next = correctedHueMap[i + 1];
|
|
172
|
+
|
|
173
|
+
if (!next) {
|
|
174
|
+
selectedRange = current;
|
|
175
|
+
break;
|
|
234
176
|
}
|
|
235
|
-
|
|
236
|
-
|
|
177
|
+
|
|
178
|
+
if (targetHue >= current.correctedHue && targetHue < next.correctedHue) {
|
|
179
|
+
selectedRange = current;
|
|
180
|
+
break;
|
|
237
181
|
}
|
|
238
|
-
return correctedHueMap[correctedHueMap.length - 1];
|
|
239
|
-
};
|
|
240
|
-
const selectedRange = findCorrectRange();
|
|
241
|
-
if (!selectedRange) {
|
|
242
|
-
console.error('Could not determine the correct hue range.');
|
|
243
|
-
return null;
|
|
244
182
|
}
|
|
245
|
-
|
|
246
|
-
return
|
|
247
|
-
baseHue,
|
|
248
|
-
correctionLightness: closestLightness,
|
|
249
|
-
correctionHueRange: selectedRange.rawHue,
|
|
250
|
-
};
|
|
183
|
+
|
|
184
|
+
return (targetHue - selectedRange.shift + 360) % 360;
|
|
251
185
|
}
|
|
252
186
|
|
|
253
|
-
export function
|
|
254
|
-
const
|
|
255
|
-
const
|
|
187
|
+
export function calcCorrectionRange(inputColorString: string, abneyData: GeneratorColorAbneyCorrection): number {
|
|
188
|
+
const colorOKLCH = toOklch(inputColorString) as { l: number; c: number; h: number };
|
|
189
|
+
const targetLightness = colorOKLCH.l * 100;
|
|
190
|
+
const targetHue = colorOKLCH.h;
|
|
191
|
+
const availableLightnessSteps = Object.keys(abneyData).map(Number);
|
|
192
|
+
const closestLightness = findClosestLightnessStep(targetLightness, availableLightnessSteps);
|
|
193
|
+
const lightnessCorrectionData = abneyData[closestLightness];
|
|
194
|
+
const correctedHueMap = Object.entries(lightnessCorrectionData).map(([rawHueStr, shift]) => {
|
|
195
|
+
const rawHue = Number(rawHueStr);
|
|
196
|
+
return {
|
|
197
|
+
rawHue,
|
|
198
|
+
shift,
|
|
199
|
+
correctedHue: (rawHue + shift + 360) % 360,
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
correctedHueMap.sort((a, b) => a.correctedHue - b.correctedHue);
|
|
204
|
+
|
|
205
|
+
let selectedRange = correctedHueMap[correctedHueMap.length - 1];
|
|
206
|
+
for (let i = 0; i < correctedHueMap.length; i++) {
|
|
207
|
+
const current = correctedHueMap[i];
|
|
208
|
+
const next = correctedHueMap[i + 1];
|
|
256
209
|
|
|
257
|
-
|
|
258
|
-
|
|
210
|
+
if (targetHue >= current.correctedHue && targetHue < next.correctedHue) {
|
|
211
|
+
selectedRange = current;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
259
214
|
}
|
|
260
|
-
|
|
215
|
+
|
|
216
|
+
return selectedRange.rawHue;
|
|
261
217
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skbkontur/colors",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6-alpha.0",
|
|
4
4
|
"author": "Kontur",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"exports": {
|
|
19
19
|
"./package.json": "./package.json",
|
|
20
20
|
".": {
|
|
21
|
-
"types": "./
|
|
22
|
-
"default": "./
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./index.js"
|
|
23
23
|
},
|
|
24
24
|
"./get-colors": {
|
|
25
|
-
"types": "./
|
|
26
|
-
"default": "./
|
|
25
|
+
"types": "./get-colors.d.ts",
|
|
26
|
+
"default": "./get-colors.js"
|
|
27
27
|
},
|
|
28
28
|
"./default-light": {
|
|
29
29
|
"types": "./default-light.d.ts",
|