Skip to content

Commit 16558d1

Browse files
committed
Allow binding to color prop
1 parent 9e2221b commit 16558d1

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/lib/ColorSelect.svelte

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,31 @@
2020
2121
export let color: Okhsl | Okhsv | Oklab | Oklch | Rgb
2222
23-
export let transform = (rgb: number[]) => rgb
23+
export let transform = (rgb: Rgb) => rgb
2424
2525
const dispatch = createEventDispatcher()
2626
const width = picker_size + slider_width + gap_size + border_size * 2
2727
const height = picker_size + border_size * 2
2828
29-
const toOKhsl = useMode(modeOkhsl)
29+
const toOkhsl = useMode(modeOkhsl)
3030
const toOkhsv = useMode(modeOkhsv)
3131
const toOklab = useMode(modeOklab)
3232
const toOklch = useMode(modeOklch)
3333
const toRgb = useMode(modeRgb)
3434
35-
$: okhsl = toOKhsl(color)!
3635
$: okhsv = toOkhsv(color)!
37-
$: oklab = toOklab(color)!
38-
$: oklch = toOklch(color)!
39-
$: rgb = toRgb(color)
4036
$: uihsv = scale_to_ui(okhsv)
37+
$: transform_fn = (
38+
color.mode === 'okhsl'
39+
? toOkhsl
40+
: color.mode === 'okhsv'
41+
? toOkhsv
42+
: color.mode === 'oklab'
43+
? toOklab
44+
: color.mode === 'oklch'
45+
? toOklch
46+
: toRgb
47+
) as typeof toRgb // this just makes the typing less finicky
4148
$: update_square(okhsv.h ?? 0)
4249
4350
// used to prevent re-drawing square for minor hue changes
@@ -90,7 +97,8 @@
9097
}
9198
9299
async function update_rgb() {
93-
dispatch('change', { rgb, okhsl, okhsv, oklab, oklch })
100+
color = transform_fn(okhsv)
101+
dispatch('change', { color })
94102
}
95103
96104
function update_sv(x: number, y: number) {
@@ -105,7 +113,7 @@
105113
106114
function update_h(x: number, y: number) {
107115
let h = clamp(y / picker_size)
108-
okhsv.h = h
116+
okhsv.h = h * 360
109117
110118
update_rgb()
111119
}

src/lib/render.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Rgb } from 'culori'
12
import { useMode, modeRgb, modeOkhsl, modeOkhsv } from 'culori/fn'
23
import { picker_size, slider_width } from './constants'
34

@@ -119,7 +120,7 @@ function upscale(lowres_data: Float32Array, data: Uint8ClampedArray) {
119120
export function render_main_image(
120121
hue: number,
121122
image: ImageData,
122-
transform: (rgb: number[]) => number[]
123+
transform: (rgb: Rgb) => Rgb
123124
) {
124125
const data = image.data
125126
const lowres_data = new Float32Array(

src/routes/+page.svelte

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<script lang="ts">
2-
import type { Rgb } from 'culori'
2+
import { formatCss, type Rgb } from 'culori/fn'
33
import ColorSelect from '$lib'
44
55
// https://medium.com/@valgaze/the-hidden-purple-memorial-in-your-web-browser-7d84813bb416
6-
let r = 102
7-
let g = 51
8-
let b = 153
9-
10-
$: color = { mode: 'rgb', r: r / 255, g: g / 255, b: b / 255 } as Rgb
6+
let color: Rgb = { mode: 'rgb', r: 0.4, g: 0.2, b: 0.6 }
117
</script>
128

139
<svelte:head>
@@ -17,32 +13,32 @@
1713
<h1 class="text-zinc-100 mx-8 mt-16 font-extrabold text-3xl">Okhsv Color Select</h1>
1814
<p class="text-zinc-400 mx-8 mt-2">HSV style color select using OKLab perceptual color space.</p>
1915

20-
<ColorSelect class="m-8 bg-zinc-600" {color} on:change={(e) => console.log(e.detail)} />
16+
<ColorSelect class="m-8 bg-zinc-600" bind:color />
2117

2218
<p class="text-zinc-400 mx-8 mt-2">Use left / right arrow keys to adjust Saturation, up / down to adjust Brightness.</p>
2319
<p class="text-zinc-400 mx-8 mt-2">Hold shift for larger steps and alt / option key to adjust Hue.</p>
2420

2521
<div class="m-12">
2622
<div class="flex items-center mt-2">
2723
<label class="w-24 text-red-700" for="red">Red</label>
28-
<input id="red" type="range" min="0" max="255" step="1" bind:value={r} />
29-
<span class="text-sm font-semibold text-zinc-300 w-12 ml-2 pl-2 py-0.5 pr-0 text-right">{Math.round(r)}</span>
24+
<input id="red" type="range" min="0" max="1" step={1/255} bind:value={color.r} />
25+
<span class="text-sm font-semibold text-zinc-300 w-12 ml-2 pl-2 py-0.5 pr-0 text-right">{Math.round(255 * color.r)}</span>
3026
</div>
3127

3228
<div class="flex items-center mt-2">
3329
<label class="w-24 text-green-600" for="green">Green</label>
34-
<input id="green" type="range" min="0" max="255" step="1" bind:value={g} />
35-
<span class="text-sm font-semibold text-zinc-300 w-12 ml-2 pl-2 py-0.5 pr-0 text-right">{Math.round(g)}</span>
30+
<input id="green" type="range" min="0" max="1" step={1/255} bind:value={color.g} />
31+
<span class="text-sm font-semibold text-zinc-300 w-12 ml-2 pl-2 py-0.5 pr-0 text-right">{Math.round(255 * color.g)}</span>
3632
</div>
3733

3834
<div class="flex items-center mt-2">
3935
<label class="w-24 text-blue-600" for="blue">Blue</label>
40-
<input id="blue" type="range" min="0" max="255" step="1" bind:value={b} />
41-
<span class="text-sm font-semibold text-zinc-300 w-12 ml-2 pl-2 py-0.5 pr-0 text-right">{Math.round(b)}</span>
36+
<input id="blue" type="range" min="0" max="1" step={1/255} bind:value={color.b} />
37+
<span class="text-sm font-semibold text-zinc-300 w-12 ml-2 pl-2 py-0.5 pr-0 text-right">{Math.round(255 * color.b)}</span>
4238
</div>
4339

4440
<div class="bg-white inline-block ml-24 my-6 rounded-[10px]">
45-
<div class="w-48 h-12 m-1 rounded-[6px]" style:background-color="rgb({r},{g},{b})" />
41+
<div class="w-48 h-12 m-1 rounded-[6px]" style:background-color={formatCss(color)} />
4642
</div>
4743

4844
<p class="text-white">See <a class="text-blue-300" href="https://bottosson.github.io/posts/colorpicker/">Okhsv and Okhsl by Björn Ottosson</a></p>

0 commit comments

Comments
 (0)