@skbkontur/colors 2.0.0-alpha.1 → 2.0.0-alpha.2

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.
@@ -7,13 +7,20 @@ export interface ColorObject {
7
7
  }
8
8
  export type ColorValue = string | ColorObject | ColorValue[];
9
9
  type ColorStructure = ColorObject | ColorValue[];
10
+ export type ColorFormat = 'hex/rgba' | 'rgb/rgba' | 'oklch' | 'hex-aarrggbb';
10
11
  export interface ConfigOptions {
11
12
  brand: PresetOrCustom<keyof typeof DEFAULT_SWATCH.brand>;
12
13
  accent: PresetOrCustom<(typeof ACCENT_PARAMS)[number]>;
13
14
  system?: typeof DEFAULT_SWATCH.system;
15
+ format?: ColorFormat;
14
16
  }
15
17
  export declare function getBaseTokens({ brand, accent, system }: ConfigOptions): BaseTokens;
16
18
  export declare function convertOklchToHex(obj: ColorStructure): ColorStructure;
17
19
  export declare const convertOklchToRgba: (oklchString: string) => string;
18
20
  export declare function convertAlphaOklchToRgba(obj: ColorStructure): ColorStructure;
21
+ export declare const convertHexToRgbString: (hexString: string) => string;
22
+ export declare function convertHexValuesToRgb(obj: ColorStructure): ColorStructure;
23
+ export declare const convertToAarrggbb: (colorString: string) => string;
24
+ export declare function convertToAarrggbbRecursive(obj: ColorStructure): ColorStructure;
25
+ export declare function convertColorFormat(obj: ColorStructure, format?: ColorFormat): ColorStructure;
19
26
  export {};
@@ -40,7 +40,12 @@ var __importStar =
40
40
  return result;
41
41
  };
42
42
  Object.defineProperty(exports, '__esModule', { value: true });
43
- exports.convertAlphaOklchToRgba =
43
+ exports.convertColorFormat =
44
+ exports.convertToAarrggbbRecursive =
45
+ exports.convertToAarrggbb =
46
+ exports.convertHexValuesToRgb =
47
+ exports.convertHexToRgbString =
48
+ exports.convertAlphaOklchToRgba =
44
49
  exports.convertOklchToRgba =
45
50
  exports.convertOklchToHex =
46
51
  exports.getBaseTokens =
@@ -184,6 +189,7 @@ function convertAlphaOklchToRgba(obj) {
184
189
  for (var key in obj) {
185
190
  if (Object.prototype.hasOwnProperty.call(obj, key)) {
186
191
  var value = obj[key];
192
+ // Конвертируем OKLCH с альфа-каналом (содержит '/') в RGBA
187
193
  if (typeof value === 'string' && value.includes('oklch(')) {
188
194
  value = (0, exports.convertOklchToRgba)(value);
189
195
  }
@@ -197,3 +203,117 @@ function convertAlphaOklchToRgba(obj) {
197
203
  return newObj;
198
204
  }
199
205
  exports.convertAlphaOklchToRgba = convertAlphaOklchToRgba;
206
+ var convertHexToRgbString = function (hexString) {
207
+ var _a;
208
+ var color = (0, culori_1.converter)('rgb')(hexString);
209
+ if (!color) {
210
+ return hexString;
211
+ }
212
+ var r = Math.round(color.r * 255);
213
+ var g = Math.round(color.g * 255);
214
+ var b = Math.round(color.b * 255);
215
+ var alpha = (_a = color.alpha) !== null && _a !== void 0 ? _a : 1;
216
+ return alpha === 1
217
+ ? 'rgb('.concat(r, ', ').concat(g, ', ').concat(b, ')')
218
+ : 'rgba('.concat(r, ', ').concat(g, ', ').concat(b, ', ').concat(alpha, ')');
219
+ };
220
+ exports.convertHexToRgbString = convertHexToRgbString;
221
+ function convertHexValuesToRgb(obj) {
222
+ if (typeof obj !== 'object' || obj === null) {
223
+ return obj;
224
+ }
225
+ if (Array.isArray(obj)) {
226
+ return obj.map(function (item) {
227
+ if (typeof item === 'object' && item !== null) {
228
+ return convertHexValuesToRgb(item);
229
+ }
230
+ return item;
231
+ });
232
+ }
233
+ var newObj = {};
234
+ for (var key in obj) {
235
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
236
+ var value = obj[key];
237
+ // Конвертируем HEX-строки в RGB(A)-строки
238
+ if (typeof value === 'string' && value.startsWith('#')) {
239
+ newObj[key] = (0, exports.convertHexToRgbString)(value);
240
+ } else if (typeof value === 'object' && value !== null) {
241
+ newObj[key] = convertHexValuesToRgb(value);
242
+ } else {
243
+ newObj[key] = value;
244
+ }
245
+ }
246
+ }
247
+ return newObj;
248
+ }
249
+ exports.convertHexValuesToRgb = convertHexValuesToRgb;
250
+ var convertToAarrggbb = function (colorString) {
251
+ var _a;
252
+ var color = (0, culori_1.converter)('rgb')(colorString);
253
+ if (!color) {
254
+ return colorString;
255
+ }
256
+ var r = Math.round(color.r * 255);
257
+ var g = Math.round(color.g * 255);
258
+ var b = Math.round(color.b * 255);
259
+ var alpha = Math.round(((_a = color.alpha) !== null && _a !== void 0 ? _a : 1) * 255);
260
+ var toHex = function (c) {
261
+ return c.toString(16).padStart(2, '0');
262
+ };
263
+ var rgbHex = ''.concat(toHex(r)).concat(toHex(g)).concat(toHex(b)).toUpperCase();
264
+ if (alpha === 255) {
265
+ return '#'.concat(rgbHex);
266
+ }
267
+ return '#'.concat(toHex(alpha)).concat(rgbHex).toUpperCase();
268
+ };
269
+ exports.convertToAarrggbb = convertToAarrggbb;
270
+ function convertToAarrggbbRecursive(obj) {
271
+ if (typeof obj !== 'object' || obj === null) {
272
+ return obj;
273
+ }
274
+ if (Array.isArray(obj)) {
275
+ return obj.map(function (item) {
276
+ if (typeof item === 'object' && item !== null) {
277
+ return convertToAarrggbbRecursive(item);
278
+ }
279
+ return item;
280
+ });
281
+ }
282
+ var newObj = {};
283
+ for (var key in obj) {
284
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
285
+ var value = obj[key];
286
+ // Конвертируем строки цветов (HEX, RGBA) в AARRGGBB
287
+ if (typeof value === 'string') {
288
+ newObj[key] = (0, exports.convertToAarrggbb)(value); // Измененная логика в этой функции
289
+ } else if (typeof value === 'object' && value !== null) {
290
+ newObj[key] = convertToAarrggbbRecursive(value);
291
+ } else {
292
+ newObj[key] = value;
293
+ }
294
+ }
295
+ }
296
+ return newObj;
297
+ }
298
+ exports.convertToAarrggbbRecursive = convertToAarrggbbRecursive;
299
+ function convertColorFormat(obj, format) {
300
+ if (format === void 0) {
301
+ format = 'hex/rgba';
302
+ }
303
+ var result = obj;
304
+ result = convertOklchToHex(obj);
305
+ result = convertAlphaOklchToRgba(result);
306
+ switch (format) {
307
+ case 'oklch':
308
+ return obj;
309
+ case 'hex/rgba':
310
+ return result;
311
+ case 'rgb/rgba':
312
+ return convertHexValuesToRgb(result);
313
+ case 'hex-aarrggbb':
314
+ return convertToAarrggbbRecursive(result);
315
+ default:
316
+ return result;
317
+ }
318
+ }
319
+ exports.convertColorFormat = convertColorFormat;
@@ -1,4 +1,4 @@
1
- import { type ConfigOptions } from './get-base-tokens';
1
+ import { type ConfigOptions, type ColorFormat } from './get-base-tokens';
2
2
  import { getDefaultTokens } from './get-default-tokens';
3
3
  import type { BaseTokens } from './types/base-tokens';
4
4
  export interface Themed<T> {
@@ -10,6 +10,7 @@ export type DefaultTokens = DefaultTokensFull['light' | 'dark'];
10
10
  export interface SemanticConfigOptions<T> extends ConfigOptions {
11
11
  theme?: 'light' | 'dark';
12
12
  overrides?: (base: BaseTokens, defaults?: DefaultTokensFull, params?: SemanticConfigOptions<T>) => Themed<T>;
13
+ format?: ColorFormat;
13
14
  }
14
15
  export declare function getColors(params: SemanticConfigOptions<DefaultTokens>): DefaultTokens;
15
16
  export declare function getColors<T>(
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.getColors = void 0;
4
- var get_base_tokens_1 = require('./get-base-tokens'); //
4
+ var get_base_tokens_1 = require('./get-base-tokens');
5
5
  var get_default_tokens_1 = require('./get-default-tokens');
6
6
  var DEFAULT_THEME = 'light';
7
7
  function getColors(params) {
@@ -9,17 +9,12 @@ function getColors(params) {
9
9
  var base = (0, get_base_tokens_1.getBaseTokens)(params);
10
10
  var defaults = (0, get_default_tokens_1.getDefaultTokens)(base);
11
11
  var result;
12
- // 1. Получаем базовый объект цветов (либо дефолтный, либо переопределенный)
13
12
  if (params.overrides) {
14
13
  result = params.overrides(base, defaults, params)[theme];
15
14
  } else {
16
15
  result = defaults[theme];
17
16
  }
18
- // 2. Конвертируем oklch без прозрачности в HEX
19
- // Используем 'as any', так как конвертеры рассчитаны на рекурсивную структуру ColorStructure
20
- var hexConverted = (0, get_base_tokens_1.convertOklchToHex)(result);
21
- // 3. Конвертируем оставшиеся oklch (с прозрачностью) в RGBA
22
- var finalResult = (0, get_base_tokens_1.convertAlphaOklchToRgba)(hexConverted);
17
+ var finalResult = (0, get_base_tokens_1.convertColorFormat)(result, params.format);
23
18
  return finalResult;
24
19
  }
25
20
  exports.getColors = getColors;
@@ -277,9 +277,9 @@ function calculateBaseHueAndCorrectionRange(inputColorString, abneyData) {
277
277
  }
278
278
  exports.calculateBaseHueAndCorrectionRange = calculateBaseHueAndCorrectionRange;
279
279
  function calcOnBrand(hex) {
280
- var whiteContrast = Number((0, apca_w3_1.calcAPCA)('#fff', hex));
281
- var blackContrast = Number((0, apca_w3_1.calcAPCA)('#000', hex));
282
- if (whiteContrast - 10 <= blackContrast) {
280
+ var whiteContrast = Math.abs(Number((0, apca_w3_1.calcAPCA)('#fff', hex)));
281
+ var blackContrast = Math.abs(Number((0, apca_w3_1.calcAPCA)('#000', hex)));
282
+ if (whiteContrast + 10 >= blackContrast) {
283
283
  return DefaultSwatch.whiteAlpha;
284
284
  }
285
285
  return DefaultSwatch.blackAlpha;