Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use colorjs.io sub-exports for perf #78

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ bench.add('real world sort example #2 (nerdy.dev)', () => {
})

bench.add('convert hex', () => convert('#f00'))
bench.add('convert hex with alpha', () => convert('#f000'))
bench.add('convert rgba()', () => convert('rgba(255,0,0,0.5)'))
bench.add('convert rgba() (modern syntax)', () => convert('rgba(255 0 0 / 0.5)'))
bench.add('convert rgb()', () => convert('rgb(255,0,0)'))
Expand All @@ -131,10 +132,13 @@ bench.add('convert hsl()', () => convert('hsl(0,100%,50%)'))
bench.add('convert hsl() (modern syntax)', () => convert('hsl(0 100% 50%)'))
bench.add('convert hsla()', () => convert('hsla(0,100%,50%,0.5)'))
bench.add('convert hsla() (modern syntax)', () => convert('hsla(0 100% 50% / 0.5)'))
bench.add('convert lch()', () => convert('lch(52.2345% 72.2 56.2 / .5)'))
bench.add('convert oklch()', () => convert('oklch(25% .148 81.72)'))
bench.add('convert color()', () => convert('color(display-p3 1 0.5 0 / .5)'))
bench.add('convert relative color()', () => convert('color(from green srgb r g b / 0.5)'))
bench.add('convert named color', () => convert('red'))
bench.add('convert transparent', () => convert('transparent'))
bench.add('convert invalid color', () => convert('invalid'))
bench.add('convert oklch()', () => convert('oklch(25% .148 81.72)'))
bench.add('convert system color', () => convert('Highlight'))

await bench.warmup()
Expand Down
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import Color from 'colorjs.io'
import {
parse,
to as convertColor,
ColorSpace,
sRGB,
P3,
LCH,
HSL,
OKLCH,
} from "colorjs.io/fn"

// Register color spaces for parsing and converting
ColorSpace.register(sRGB) // Can parse keywords and hex colors
ColorSpace.register(P3)
ColorSpace.register(HSL)
ColorSpace.register(LCH)
ColorSpace.register(OKLCH)

/**
* @typedef NormalizedColor
Expand Down Expand Up @@ -32,11 +48,10 @@ function numerify(value) {
* @returns {NormalizedColor & { authored: string }}
*/
export function convert(authored) {
// TODO: get rid of try/catch
// TODO: use Color.js's faster exports
try {
let converted = new Color(authored)
let hsl = converted.hsl
let parsed = parse(authored)
let converted = parsed.spaceId === 'hsl' ? parsed : convertColor(parsed, HSL)
let hsl = converted.coords
let hue = numerify(hsl[0])
let saturation = numerify(hsl[1])
let lightness = numerify(hsl[2])
Expand Down
17 changes: 9 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ test('the convert fn converts colors to an HSLA object', () => {
'hsla(0, 100%, 50%, 1)',
'hsl(0, 100%, 50%)',
'rgb(255, 0, 0)',
'rgba(255, 0, 0, 1)'
'rgba(255, 0, 0, 1)',
'oklch(62.8% 0.25768330773615683 29.2338851923426)'
]

for (let color of colors) {
assert.equal(convert(color), {
hue: 0,
saturation: 100,
lightness: 50,
alpha: 1,
authored: color
}, `Failed convert for '${color}'`)
let converted = convert(color)
// Making sure most colors are mostly within the range
assert.ok(converted.hue >= 0 && converted.hue <= 0.01, `Failed hue for '${color}', got ${converted.hue}`)
assert.ok(converted.saturation >= 99.9 && converted.saturation <= 100.02, `Failed saturation for '${color}', got ${converted.saturation}`)
assert.ok(converted.lightness >= 49.9 && converted.lightness <= 50.02, `Failed lightness for '${color}', got ${converted.lightness}`)
assert.equal(converted.alpha, 1, `Failed alpha for '${color}'`)
assert.equal(converted.authored, color, `Failed authored for '${color}'`)
}
})

Expand Down
Loading