Skip to content

Commit

Permalink
Fix hexToRGB util and prefers-color-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
taysea committed Feb 27, 2024
1 parent 712101b commit 2ae02f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
39 changes: 21 additions & 18 deletions design-tokens/src/color.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
import { Color } from './figma_api.js'
import { Color } from './figma_api.js';

/**
* Compares two colors for approximate equality since converting between Figma RGBA objects (from 0 -> 1) and
* hex colors can result in slight differences.
*/
export function colorApproximatelyEqual(colorA: Color, colorB: Color) {
return rgbToHex(colorA) === rgbToHex(colorB)
return rgbToHex(colorA) === rgbToHex(colorB);
}

export function parseColor(color: string): Color {
color = color.trim()
const hexRegex = /^#([A-Fa-f0-9]{6})([A-Fa-f0-9]{2}){0,1}$/
const hexShorthandRegex = /^#([A-Fa-f0-9]{3})([A-Fa-f0-9]){0,1}$/
color = color.trim();
const hexRegex = /^#([A-Fa-f0-9]{6})([A-Fa-f0-9]{2}){0,1}$/;
const hexShorthandRegex = /^#([A-Fa-f0-9]{3})([A-Fa-f0-9]){0,1}$/;

if (hexRegex.test(color) || hexShorthandRegex.test(color)) {
const hexValue = color.substring(1)
const hexValue = color.substring(1);
const expandedHex =
hexValue.length === 3 || hexValue.length === 4
? hexValue
.split('')
.map((char) => char + char)
.map(char => char + char)
.join('')
: hexValue
: hexValue;

const alphaValue = expandedHex.length === 8 ? expandedHex.slice(6, 8) : undefined
const alphaValue =
expandedHex.length === 8 ? expandedHex.slice(6, 8) : undefined;

return {
r: parseInt(expandedHex.slice(0, 2), 16) / 255,
g: parseInt(expandedHex.slice(2, 4), 16) / 255,
b: parseInt(expandedHex.slice(4, 6), 16) / 255,
...(alphaValue ? { a: parseInt(alphaValue, 16) / 255 } : {}),
}
...(alphaValue
? { a: Math.round((parseInt(alphaValue, 16) / 255) * 100) / 100 }
: {}),
};
} else {
throw new Error('Invalid color format')
throw new Error('Invalid color format');
}
}

export function rgbToHex({ r, g, b, a }: Color) {
if (a === undefined) {
a = 1
a = 1;
}

const toHex = (value: number) => {
const hex = Math.round(value * 255).toString(16)
return hex.length === 1 ? '0' + hex : hex
}
const hex = Math.round(value * 255).toString(16);
return hex.length === 1 ? '0' + hex : hex;
};

const hex = [toHex(r), toHex(g), toHex(b)].join('')
return `#${hex}` + (a !== 1 ? toHex(a) : '')
const hex = [toHex(r), toHex(g), toHex(b)].join('');
return `#${hex}` + (a !== 1 ? toHex(a) : '');
}
2 changes: 1 addition & 1 deletion sandbox/native-web/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'design-tokens/dist/css/colors-dark.css';
import 'design-tokens/dist/css/colors.css';
import 'design-tokens/dist/css/colors-dark.css';
import 'design-tokens/dist/css/dimensions-large.css';
import 'design-tokens/dist/css/dimensions-small.css';
import './css/components.css';
Expand Down

0 comments on commit 2ae02f7

Please sign in to comment.