verdigris/tests/helpers/color.test.ts
2025-09-16 21:26:19 -06:00

86 lines
2 KiB
TypeScript

import { expect, test } from 'bun:test'
import Color from '../../src/helpers/color'
test('throws if given an empty name', () => {
expect(
() => new Color('', { l: 0, c: 0, h: 0 }))
.toThrow(
new Error('name may not be empty')
)
})
test('can get name', () => {
const color = new Color('foo', { l: 0, c: 0, h: 0 })
expect(color.name).toBe('foo')
})
test('can get Oklch values', () => {
const color = new Color('foo', { l: 1, c: 2, h: 3 })
expect(color.l).toBe(1)
expect(color.c).toBe(2)
expect(color.h).toBe(3)
})
test('throws if l under range', () => {
expect(
() => new Color('foo', { l: -1, c: 0, h: 0 }))
.toThrow(
new Error('l must be above 0')
)
})
test('throws if l above range', () => {
expect(
() => new Color('foo', { l: 101, c: 0, h: 0 }))
.toThrow(
new Error('l may not be over 100')
)
})
test('throws if c under range', () => {
expect(
() => new Color('foo', { l: 0, c: -1, h: 0 }))
.toThrow(
new Error('c must be above 0')
)
})
test('throws if c above range', () => {
expect(
() => new Color('foo', { l: 0, c: 33, h: 0 }))
.toThrow(
new Error('c may not be over 32')
)
})
test('throws if h under range', () => {
expect(
() => new Color('foo', { l: 0, c: 0, h: -1 }))
.toThrow(
new Error('h must be above 0')
)
})
test('throws if h above range', () => {
expect(
() => new Color('foo', { l: 0, c: 0, h: 361 }))
.toThrow(
new Error('h may not be over 360')
)
})
test('can get Oklch object with values', () => {
const color = new Color('foo', { l: 0, c: 0, h: 0 })
expect(color.asOklch()).toEqual({ l: 0, c: 0, h: 0 })
})
test('can convert to expected hex values', () => {
const black = new Color('foo', { l: 0, c: 0, h: 0 })
expect(black.toHex()).toBe('#000000')
const yellow = new Color('foo', { l: 100, c: 32, h: 110 })
expect(yellow.toHex()).toBe('#FFFF00')
const white = new Color('foo', { l: 100, c: 0, h: 0 })
expect(white.toHex()).toBe('#FFFFFF')
})