palette-shader 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -14
- package/dist/palette-shader.d.ts +7 -5
- package/dist/palette-shader.js +283 -197
- package/dist/palette-shader.js.map +1 -1
- package/dist/palette-shader.umd.cjs +56 -49
- package/dist/palette-shader.umd.cjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +168 -95
- package/src/shaders/hwb2rgb.frag.glsl +3 -2
- package/src/shaders/oklab.frag.glsl +1 -1
- package/src/shaders/srgb2rgb.frag.glsl +3 -4
- package/src/types.ts +2 -2
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ So if one of your palette colors only claims a tiny sliver, it lives very close
|
|
|
24
24
|
npm install palette-shader
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
No dependencies — only a browser with
|
|
27
|
+
No runtime dependencies — only a browser with WebGL2 support is required. Colors must be passed as `[r, g, b]` arrays with values in the `0–1` range (linear sRGB). Use a library such as [culori](https://culorijs.org/) to convert from CSS strings if needed.
|
|
28
28
|
|
|
29
29
|
---
|
|
30
30
|
|
|
@@ -32,17 +32,21 @@ No dependencies — only a browser with WebGL support is required.
|
|
|
32
32
|
|
|
33
33
|
```js
|
|
34
34
|
import { PaletteViz } from 'palette-shader';
|
|
35
|
+
import { converter } from 'culori';
|
|
36
|
+
|
|
37
|
+
const toSRGB = converter('srgb');
|
|
38
|
+
const toRGB = (hex) => { const c = toSRGB(hex); return [c.r, c.g, c.b]; };
|
|
35
39
|
|
|
36
40
|
// option A — pass a container, canvas is appended automatically
|
|
37
41
|
const viz = new PaletteViz({
|
|
38
|
-
palette: ['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51'],
|
|
42
|
+
palette: ['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51'].map(toRGB),
|
|
39
43
|
container: document.querySelector('#app'),
|
|
40
44
|
width: 512,
|
|
41
45
|
height: 512,
|
|
42
46
|
});
|
|
43
47
|
|
|
44
48
|
// option B — no container, place the canvas yourself
|
|
45
|
-
const viz = new PaletteViz({ palette: ['#264653', '#2a9d8f', '#e9c46a'] });
|
|
49
|
+
const viz = new PaletteViz({ palette: ['#264653', '#2a9d8f', '#e9c46a'].map(toRGB) });
|
|
46
50
|
document.querySelector('#app').appendChild(viz.canvas);
|
|
47
51
|
```
|
|
48
52
|
|
|
@@ -58,7 +62,7 @@ All options are optional. The palette defaults to a random 20-colour set.
|
|
|
58
62
|
|
|
59
63
|
| Option | Type | Default | Description |
|
|
60
64
|
| ---------------- | ------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------ |
|
|
61
|
-
| `palette` | `
|
|
65
|
+
| `palette` | `[number, number, number][]` | random | sRGB colours as `[r, g, b]` arrays, each component in the `0–1` range |
|
|
62
66
|
| `container` | `HTMLElement` | `undefined` | Element the canvas is appended to. Omit and use `viz.canvas` to place it yourself |
|
|
63
67
|
| `width` | `number` | `512` | Canvas width in CSS pixels |
|
|
64
68
|
| `height` | `number` | `512` | Canvas height in CSS pixels |
|
|
@@ -78,13 +82,14 @@ All options are optional. The palette defaults to a random 20-colour set.
|
|
|
78
82
|
Every constructor option is also a live setter/getter. Assigning any of them re-renders immediately via `requestAnimationFrame`.
|
|
79
83
|
|
|
80
84
|
```js
|
|
81
|
-
viz.palette = [
|
|
85
|
+
viz.palette = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
82
86
|
viz.position = 0.5;
|
|
83
87
|
viz.colorModel = 'okhslPolar';
|
|
84
88
|
viz.distanceMetric = 'deltaE2000';
|
|
85
89
|
viz.invertZ = true;
|
|
86
90
|
viz.showRaw = true;
|
|
87
91
|
viz.outlineWidth = 2; // transparent border between regions, in physical pixels
|
|
92
|
+
viz.pixelRatio = window.devicePixelRatio; // update after display changes
|
|
88
93
|
```
|
|
89
94
|
|
|
90
95
|
Additional read-only properties:
|
|
@@ -112,7 +117,7 @@ window.addEventListener('resize', () => viz.resize(window.innerWidth * 0.5));
|
|
|
112
117
|
Update a single palette entry without rebuilding the whole texture.
|
|
113
118
|
|
|
114
119
|
```js
|
|
115
|
-
viz.setColor(
|
|
120
|
+
viz.setColor([0.902, 0.224, 0.275], 2);
|
|
116
121
|
```
|
|
117
122
|
|
|
118
123
|
### `addColor(color, index?)`
|
|
@@ -120,17 +125,17 @@ viz.setColor('#e63946', 2);
|
|
|
120
125
|
Insert a colour at `index` (appends if omitted).
|
|
121
126
|
|
|
122
127
|
```js
|
|
123
|
-
viz.addColor(
|
|
124
|
-
viz.addColor(
|
|
128
|
+
viz.addColor([0.659, 0.855, 0.863]); // append
|
|
129
|
+
viz.addColor([0.271, 0.482, 0.616], 0); // prepend
|
|
125
130
|
```
|
|
126
131
|
|
|
127
132
|
### `removeColor(index | color)`
|
|
128
133
|
|
|
129
|
-
Remove a palette entry by index or by colour
|
|
134
|
+
Remove a palette entry by index or by colour value.
|
|
130
135
|
|
|
131
136
|
```js
|
|
132
137
|
viz.removeColor(0);
|
|
133
|
-
viz.removeColor(
|
|
138
|
+
viz.removeColor([0.659, 0.855, 0.863]);
|
|
134
139
|
```
|
|
135
140
|
|
|
136
141
|
### `destroy()`
|
|
@@ -255,7 +260,7 @@ viz.canvas.style.borderRadius = '50%';
|
|
|
255
260
|
### Multiple synchronised views
|
|
256
261
|
|
|
257
262
|
```js
|
|
258
|
-
const palette = ['#264653', '#2a9d8f', '#e9c46a'];
|
|
263
|
+
const palette = ['#264653', '#2a9d8f', '#e9c46a'].map(toRGB);
|
|
259
264
|
const shared = { palette, width: 256, height: 256, container: document.querySelector('#views') };
|
|
260
265
|
|
|
261
266
|
const views = [
|
|
@@ -277,7 +282,7 @@ document.querySelector('#slider').addEventListener('input', (e) => {
|
|
|
277
282
|
|
|
278
283
|
```js
|
|
279
284
|
const viz = new PaletteViz({
|
|
280
|
-
palette,
|
|
285
|
+
palette: ['#264653', '#2a9d8f', '#e9c46a'].map(toRGB),
|
|
281
286
|
outlineWidth: 2,
|
|
282
287
|
container: document.querySelector('#app'),
|
|
283
288
|
});
|
|
@@ -300,7 +305,7 @@ import { paletteToRGBA, randomPalette, fragmentShader } from 'palette-shader';
|
|
|
300
305
|
|
|
301
306
|
// Get raw RGBA bytes (Uint8Array, sRGB, 4 bytes per color)
|
|
302
307
|
// Useful for building your own WebGL texture or processing palette data
|
|
303
|
-
const rgba = paletteToRGBA([
|
|
308
|
+
const rgba = paletteToRGBA([[1, 0, 0], [0, 1, 0], [0, 0, 1]]);
|
|
304
309
|
|
|
305
310
|
// Quick random palette for prototyping
|
|
306
311
|
const palette = randomPalette(16);
|
|
@@ -313,7 +318,7 @@ console.log(fragmentShader);
|
|
|
313
318
|
|
|
314
319
|
## Dependencies
|
|
315
320
|
|
|
316
|
-
None. The library uses raw WebGL 2
|
|
321
|
+
None. The library uses raw WebGL 2 with no runtime dependencies. Colors are accepted as `[r, g, b]` arrays (0–1 sRGB) — no CSS parsing happens at runtime.
|
|
317
322
|
|
|
318
323
|
## Browser support
|
|
319
324
|
|
package/dist/palette-shader.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
declare type Axis = 'x' | 'y' | 'z';
|
|
2
2
|
|
|
3
|
-
declare type ColorList =
|
|
3
|
+
declare type ColorList = ColorRGB[];
|
|
4
4
|
|
|
5
|
-
declare type
|
|
5
|
+
declare type ColorRGB = [number, number, number];
|
|
6
6
|
|
|
7
7
|
declare type DistanceMetric = 'rgb' | 'oklab' | 'deltaE76' | 'deltaE94' | 'deltaE2000' | 'kotsarenkoRamos' | 'oklrab' | 'cielabD50';
|
|
8
8
|
|
|
@@ -22,10 +22,10 @@ export declare class PaletteViz {
|
|
|
22
22
|
destroy(): void;
|
|
23
23
|
set palette(palette: ColorList);
|
|
24
24
|
get palette(): ColorList;
|
|
25
|
-
setColor(color:
|
|
26
|
-
addColor(color:
|
|
25
|
+
setColor(color: ColorRGB, index: number): void;
|
|
26
|
+
addColor(color: ColorRGB, index?: number): void;
|
|
27
27
|
removeColor(index: number): void;
|
|
28
|
-
removeColor(color:
|
|
28
|
+
removeColor(color: ColorRGB): void;
|
|
29
29
|
set position(value: number);
|
|
30
30
|
get position(): number;
|
|
31
31
|
set axis(axis: Axis);
|
|
@@ -38,6 +38,8 @@ export declare class PaletteViz {
|
|
|
38
38
|
get invertZ(): boolean;
|
|
39
39
|
set showRaw(value: boolean);
|
|
40
40
|
get showRaw(): boolean;
|
|
41
|
+
set pixelRatio(value: number);
|
|
42
|
+
get pixelRatio(): number;
|
|
41
43
|
set outlineWidth(value: number);
|
|
42
44
|
get outlineWidth(): number;
|
|
43
45
|
static paletteToRGBA: (palette: ColorList) => Uint8Array;
|