Skip to content

Commit

Permalink
Fix(exporter-js,exporter-scss): Refactor color normalization to make …
Browse files Browse the repository at this point in the history
…it actually work #DS-1394
  • Loading branch information
crishpeen committed Jul 23, 2024
1 parent 80f8f2b commit 4388423
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 112 deletions.
2 changes: 1 addition & 1 deletion exporters/js/exporter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Jan Kryšpín <[email protected]>",
"organization": "LMC s.r.o.",
"source_dir": "src",
"version": "2.0.0",
"version": "2.0.1",
"usesBrands": true,
"config": {
"sources": "sources.json",
Expand Down
38 changes: 19 additions & 19 deletions exporters/js/generated/functions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions exporters/js/src/js/formatters/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ type ColorShape = {
};

export function formatColor(color: ColorShape): string {
if (color.a < 255) {
return `#${color.hex}`;
}

return `#${normalizeColor(color.hex.substring(0, 6))}`;
return normalizeColor(color.hex);
}
11 changes: 7 additions & 4 deletions exporters/js/src/js/normalizers/__tests__/color.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { normalizeColor } from '../color';
describe('normalizeColor', () => {
it.each([
// name, expected
['123456', '123456'],
['fff', 'fff'],
['ffffff', 'fff'],
['ffffff00', 'fff0'],
['123456', '#123456'],
['123', '#123'],
['fff', '#fff'],
['ffffff', '#fff'],
['ffffff00', '#fff0'],
['ffffffff', '#fff'],
['fffffff0', '#fffffff0'],
])('should normalize color', (color, expected) => {
expect(normalizeColor(color)).toBe(expected);
});
Expand Down
59 changes: 33 additions & 26 deletions exporters/js/src/js/normalizers/color.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
const COLOR_HEX_LENGTH = {
SHORT: 'short',
LONG: 'long',
};

type ColorHexLength = (typeof COLOR_HEX_LENGTH)[keyof typeof COLOR_HEX_LENGTH];

export function normalizeColor(color: string, colorHexLength: ColorHexLength = COLOR_HEX_LENGTH.SHORT): string {
const colorParts = color.match(/.{1,2}/g);
let shortHex = colorHexLength === COLOR_HEX_LENGTH.SHORT;
colorParts &&
colorParts.forEach((part) => {
if (colorHexLength === COLOR_HEX_LENGTH.SHORT) {
shortHex = /^(.)\1+$/.test(part);
}
});

if (shortHex) {
return `${color.substring(0, 1)}${color.substring(2, 3)}${color.substring(4, 5)}${color.substring(6, 7)}`;
}
const SHORT_HEX_WITHOUT_ALPHA_LENGTH = 3;
const SHORT_HEX_WITH_ALPHA_LENGTH = 4;
const LONG_HEX_WITH_ALPHA_LENGTH = 8;

export function normalizeColor(hexCode: string): string {
const isShortHex =
hexCode.length === SHORT_HEX_WITHOUT_ALPHA_LENGTH || hexCode.length === SHORT_HEX_WITH_ALPHA_LENGTH;

const canHexBeShortened = (hex: string) =>
hex.length % 2 === 0 && hex.split('').every((ch, i, arr) => (i % 2 === 0 ? ch === arr[i + 1] : true));

if (colorHexLength === COLOR_HEX_LENGTH.LONG && color.length === 3) {
// Convert short hex to long hex
// @see: https://www.30secondsofcode.org/js/s/extend-hex/
return color
const shortHex = (hex: string) =>
hex
.split('')
.map((x) => x + x)
.map((char, index) => (index % 2 === 0 ? char : ''))
.join('');

let processedHex: string;

if (isShortHex) {
processedHex = hexCode;
} else if (canHexBeShortened(hexCode)) {
processedHex = shortHex(hexCode);
} else {
processedHex = hexCode;
}

// Remove alpha channel if it's 255 aka ff
if (processedHex.length === LONG_HEX_WITH_ALPHA_LENGTH && processedHex.endsWith('ff')) {
return `#${processedHex.slice(0, -2)}`;
}

// Remove alpha channel if it's 255 aka f (in short form)
if (processedHex.length === SHORT_HEX_WITH_ALPHA_LENGTH && processedHex.endsWith('f')) {
return `#${processedHex.slice(0, -1)}`;
}

return color;
return `#${processedHex}`;
}
2 changes: 1 addition & 1 deletion exporters/scss/exporter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Jan Kryšpín <[email protected]>",
"organization": "LMC s.r.o.",
"source_dir": "src",
"version": "3.0.0",
"version": "3.0.1",
"usesBrands": true,
"config": {
"sources": "sources.json",
Expand Down
Loading

0 comments on commit 4388423

Please sign in to comment.