Skip to content

Commit 4113491

Browse files
committed
Move to float L in other parts of UI
1 parent cc73337 commit 4113491

File tree

10 files changed

+36
-35
lines changed

10 files changed

+36
-35
lines changed

config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ let config = {
1515
H_STEP: 1,
1616

1717
L_MAX: 1,
18-
L_STEP: 1
18+
L_MAX_COLOR: 1,
19+
L_STEP: 0.01
1920
}
2021

2122
if (process.env.LCH) {
@@ -29,7 +30,7 @@ if (process.env.LCH) {
2930

3031
COLOR_FN: '"lch"',
3132

32-
L_MAX: 100
33+
L_MAX_COLOR: 100
3334
}
3435
}
3536

lib/model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function getModelData(mode: RgbMode): [Vector3[], number[]] {
4444
if (to.h) {
4545
colors.push(edgeRgb.r, edgeRgb.g, edgeRgb.b)
4646
coordinates.push(
47-
new Vector3(to.l / L_MAX, to.c / (C_MAX * 2), to.h / 360)
47+
new Vector3(to.l / L_MAX_COLOR, to.c / (C_MAX * 2), to.h / 360)
4848
)
4949
}
5050
}
@@ -127,7 +127,7 @@ function generateMesh(scene: Scene, mode: RgbMode): UpdateSlice {
127127
if ('array' in bottom.attributes.position) {
128128
let bottomSteps = bottom.attributes.position.array.length / 6
129129
for (let i = 0; i <= bottomSteps; i += 1) {
130-
let lchL = (L_MAX * i) / bottomSteps
130+
let lchL = (L_MAX_COLOR * i) / bottomSteps
131131
let rgbL = rgb(build(lchL, 0, 0)).r
132132
bottomColors.push(rgbL, rgbL, rgbL, rgbL, rgbL, rgbL)
133133
}
@@ -140,7 +140,7 @@ function generateMesh(scene: Scene, mode: RgbMode): UpdateSlice {
140140
scene.add(bottomMesh)
141141

142142
return color => {
143-
l.set(0, 0.01 * -color.l + 0.5)
143+
l.set(0, -color.l + 0.5)
144144
c.set(0, (0.5 * -color.c) / C_MAX + 0.5)
145145
h.set(0, 0.0028 * color.h - (color.h > 350 ? 0.51 : 0.5))
146146
}

stores/current.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface LchValue {
2828
type PrevCurrentValue = { [key in keyof LchValue]?: undefined } | LchValue
2929

3030
function randomColor(): LchValue {
31-
return { a: 100, c: C_RANDOM, h: Math.round(360 * Math.random()), l: 70 }
31+
return { a: 100, c: C_RANDOM, h: Math.round(360 * Math.random()), l: 0.7 }
3232
}
3333

3434
function parseHash(): LchValue | undefined {
@@ -159,7 +159,7 @@ function aggressiveRoundValue<V extends Partial<LchValue>>(
159159
): V {
160160
let rounded = { ...value }
161161
if (typeof rounded.l !== 'undefined') {
162-
rounded.l = round2(rounded.l)
162+
rounded.l = type === 'oklch' ? round4(rounded.l) : round2(rounded.l)
163163
}
164164
if (typeof rounded.c !== 'undefined') {
165165
rounded.c = type === 'oklch' ? round4(rounded.c) : round2(rounded.c)
@@ -179,7 +179,7 @@ function preciseRoundValue<V extends Partial<LchValue>>(
179179
): V {
180180
let rounded = { ...value }
181181
if (typeof rounded.l !== 'undefined') {
182-
rounded.l = round4(rounded.l)
182+
rounded.l = type === 'oklch' ? round6(rounded.l) : round4(rounded.l)
183183
}
184184
if (typeof rounded.c !== 'undefined') {
185185
rounded.c = type === 'oklch' ? round6(rounded.c) : round4(rounded.c)
@@ -208,7 +208,7 @@ export function setCurrent(code: string, isRgbInput = false): boolean {
208208
parsed.g === 1 &&
209209
parsed.b === 1
210210
) {
211-
current.set({ a: (parsed.alpha ?? 1) * 100, c: 0, h: 0, l: 100 })
211+
current.set({ a: (parsed.alpha ?? 1) * 100, c: 0, h: 0, l: 1 })
212212
return true
213213
}
214214
let originSpace = getSpace(parsed)
@@ -249,15 +249,15 @@ export function setCurrent(code: string, isRgbInput = false): boolean {
249249
}
250250

251251
export function valueToColor(value: LchValue): AnyLch {
252-
return build((L_MAX * value.l) / 100, value.c, value.h, value.a / 100)
252+
return build(value.l * L_MAX_COLOR, value.c, value.h, value.a / 100)
253253
}
254254

255255
export function colorToValue(color: AnyLch): LchValue {
256256
return {
257257
a: (color.alpha ?? 1) * 100,
258258
c: color.c,
259259
h: color.h ?? 0,
260-
l: (100 * color.l) / L_MAX
260+
l: color.l / L_MAX_COLOR
261261
}
262262
}
263263

test/colors.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('correctly works with colors on the edges of sRGB', () => {
2222
a: 100,
2323
c: 0.2577,
2424
h: 29.23,
25-
l: 62.8
25+
l: 0.628
2626
})
2727
deepStrictEqual(visible.get().space, 'srgb')
2828

@@ -31,7 +31,7 @@ test('correctly works with colors on the edges of sRGB', () => {
3131
a: 100,
3232
c: 0.294827,
3333
h: 142.4953,
34-
l: 86.644
34+
l: 0.86644
3535
})
3636
deepStrictEqual(visible.get().space, 'srgb')
3737

@@ -40,7 +40,7 @@ test('correctly works with colors on the edges of sRGB', () => {
4040
a: 100,
4141
c: 0.313214,
4242
h: 264.052,
43-
l: 45.2014
43+
l: 0.452014
4444
})
4545
deepStrictEqual(visible.get().space, 'srgb')
4646

@@ -49,7 +49,7 @@ test('correctly works with colors on the edges of sRGB', () => {
4949
a: 100,
5050
c: 0.15455,
5151
h: 194.7689,
52-
l: 90.5399
52+
l: 0.905399
5353
})
5454
deepStrictEqual(visible.get().space, 'srgb')
5555

@@ -58,7 +58,7 @@ test('correctly works with colors on the edges of sRGB', () => {
5858
a: 100,
5959
c: 0.3225,
6060
h: 328.36,
61-
l: 70.17
61+
l: 0.7017
6262
})
6363
deepStrictEqual(visible.get().space, 'srgb')
6464

@@ -67,7 +67,7 @@ test('correctly works with colors on the edges of sRGB', () => {
6767
a: 100,
6868
c: 0.211,
6969
h: 109.77,
70-
l: 96.8
70+
l: 0.968
7171
})
7272
deepStrictEqual(visible.get().space, 'srgb')
7373

@@ -76,7 +76,7 @@ test('correctly works with colors on the edges of sRGB', () => {
7676
a: 100,
7777
c: 0,
7878
h: 0,
79-
l: 100
79+
l: 1
8080
})
8181
deepStrictEqual(visible.get().space, 'srgb')
8282
})

test/inputs.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ import { current, setCurrent } from '../stores/current.ts'
77

88
test('understands CSS declaration', () => {
99
setCurrent('color: oklch(80% 0.1 195);')
10-
deepStrictEqual(current.get(), { a: 100, c: 0.1, h: 195, l: 80 })
10+
deepStrictEqual(current.get(), { a: 100, c: 0.1, h: 195, l: 0.8 })
1111

1212
setCurrent('color: #f00')
13-
deepStrictEqual(current.get(), { a: 100, c: 0.2577, h: 29.23, l: 62.8 })
13+
deepStrictEqual(current.get(), { a: 100, c: 0.2577, h: 29.23, l: 0.628 })
1414
})
1515

1616
test('understands hex without #', () => {
1717
setCurrent('f00')
18-
deepStrictEqual(current.get(), { a: 100, c: 0.2577, h: 29.23, l: 62.8 })
18+
deepStrictEqual(current.get(), { a: 100, c: 0.2577, h: 29.23, l: 0.628 })
1919

2020
setCurrent('ff0000')
21-
deepStrictEqual(current.get(), { a: 100, c: 0.2577, h: 29.23, l: 62.8 })
21+
deepStrictEqual(current.get(), { a: 100, c: 0.2577, h: 29.23, l: 0.628 })
2222
})
2323

2424
test('understands OKLCH without function', () => {
2525
setCurrent('80% 0.1 195')
26-
deepStrictEqual(current.get(), { a: 100, c: 0.1, h: 195, l: 80 })
26+
deepStrictEqual(current.get(), { a: 100, c: 0.1, h: 195, l: 0.8 })
2727

2828
setCurrent('0.8 0.1 195')
29-
deepStrictEqual(current.get(), { a: 100, c: 0.1, h: 195, l: 80 })
29+
deepStrictEqual(current.get(), { a: 100, c: 0.1, h: 195, l: 0.8 })
3030
})

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare module '*?worker' {
1414
declare const COLOR_FN: 'lch' | 'oklch'
1515
declare const LCH: boolean
1616
declare const L_MAX: number
17+
declare const L_MAX_COLOR: number
1718
declare const L_STEP: number
1819
declare const C_MAX: number
1920
declare const C_MAX_REC2020: number

view/card/mixin.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ mixin card(type)
1010
type: 'text',
1111
role: 'spinbutton',
1212
'aria-valuemin': 0,
13-
'aria-valuemax': 100,
13+
'aria-valuemax': 1,
1414
pattern: '^[0-9+\\/*.\\-]+$',
1515
inputmode: 'decimal',
1616
step: L_STEP
1717
})
1818
.card_chart
1919
.card_size
2020
+chart('h', 'c', 'l')
21-
+range('l', 100, L_STEP, 70)
21+
+range('l', 1, L_STEP, 70)
2222

2323
else if type === 'c'
2424
h2.card_title Chroma

view/chart/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ onPaint({
3131
document.body.style.setProperty('--chart-h', `${(100 * h) / H_MAX}%`)
3232
},
3333
l(l) {
34-
document.body.style.setProperty('--chart-l', `${l}%`)
34+
document.body.style.setProperty('--chart-l', `${(100 * l) / L_MAX}%`)
3535
}
3636
})
3737

@@ -51,12 +51,12 @@ function setComponentsFromSpace(
5151
} else if (space.parentElement!.classList.contains('is-c')) {
5252
setCurrentComponents({
5353
h: (H_MAX * x) / rect.width,
54-
l: (100 * y) / rect.height
54+
l: (L_MAX * y) / rect.height
5555
})
5656
} else if (space.parentElement!.classList.contains('is-h')) {
5757
setCurrentComponents({
5858
c: (getMaxC() * y) / rect.height,
59-
l: (100 * x) / rect.width
59+
l: (L_MAX * x) / rect.width
6060
})
6161
}
6262
}
@@ -158,7 +158,7 @@ function initCharts(): void {
158158
},
159159
l(l, chartsToChange) {
160160
if (!showCharts.get()) return
161-
startWorkForComponent(canvasL, 'l', (L_MAX * l) / 100, chartsToChange)
161+
startWorkForComponent(canvasL, 'l', l, chartsToChange)
162162
}
163163
})
164164
}

view/chart/paint.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function paintCL(
124124
borderP3: Rgb,
125125
borderRec2020: Rgb
126126
): ImageData {
127-
let lFactor = L_MAX / width
127+
let lFactor = L_MAX_COLOR / width
128128
let cFactor = (showRec2020 ? C_MAX_REC2020 : C_MAX) / height
129129

130130
return paint(
@@ -165,7 +165,7 @@ export function paintCH(
165165
showRec2020,
166166
borderP3,
167167
borderRec2020,
168-
(x, y) => build(l, y * cFactor, x * hFactor)
168+
(x, y) => build(L_MAX_COLOR * l, y * cFactor, x * hFactor)
169169
)
170170
}
171171

@@ -181,7 +181,7 @@ export function paintLH(
181181
borderRec2020: Rgb
182182
): ImageData {
183183
let hFactor = H_MAX / width
184-
let lFactor = L_MAX / height
184+
let lFactor = L_MAX_COLOR / height
185185

186186
return paint(
187187
height,

view/range/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function paint(
6161
function addStop(x: number, round: (num: number) => number): void {
6262
let origin = getColor(x)
6363
let value = origin[type] ?? 0
64-
if (type === 'l') value = (100 / L_MAX) * value
6564
stops.push(round(value / sliderStep) * sliderStep)
6665
}
6766

@@ -149,7 +148,7 @@ onPaint({
149148
let c = color.c
150149
let h = color.h ?? 0
151150
let [width, height] = initCanvasSize(canvasL)
152-
let factor = L_MAX / width
151+
let factor = L_MAX_COLOR / width
153152
setList(
154153
listL,
155154
paint(canvasL, 'l', width, height, parseFloat(inputL.step), x => {

0 commit comments

Comments
 (0)