@ateliercartographie/ok-palette 0.1.0 → 0.1.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ateliercartographie/ok-palette
2
2
 
3
- Générateur de palettes de couleurs **séquentielles, divergentes et catégorielles** pour la **dataviz** et la **cartographie**. Génère aussi des **palettes de motifs** compatibles avec [@ateliercartographie/motif.js](https://github.com/AtelierCartographie/motif.js).
3
+ Color palette generator for **sequential, diverging, and categorical** schemes for **data visualization** and **cartography**. Also generates **pattern palettes** compatible with [@ateliercartographie/motif.js](https://github.com/AtelierCartographie/motif.js).
4
4
 
5
5
  Zero dependencies. Relies entirely on modern browser CSS capabilities (`oklch`, `color-mix()`) to guarantee perceptually uniform results.
6
6
 
@@ -40,7 +40,7 @@ const biTone = sequential({
40
40
  contrast: "high",
41
41
  });
42
42
 
43
- // Symmetric diverging palette (3+1+3 classes)
43
+ // Symmetric diverging palette (3+3 classes)
44
44
  const div = divergent({ colorA: "#3b4cc0" });
45
45
 
46
46
  // Asymmetric diverging palette, oklch interpolation
@@ -110,7 +110,7 @@ Generates a diverging (bipolar) palette around a tinted pivot.
110
110
  | `colorB` | `string?` | complement of `colorA` | Right-extreme CSS color |
111
111
  | `steps` | `[number, number]` | `[3, 3]` | Classes per side `[left, right]` |
112
112
  | `contrast` | `"low" \| "normal" \| "high"` | `"normal"` | Contrast profile |
113
- | `hasCenterClass` | `boolean` | `true` | Add a neutral center class |
113
+ | `hasCenterClass` | `boolean` | `false` | Add a neutral center class |
114
114
  | `colorSpace` | `"oklab" \| "oklch"` | `"oklab"` | Ramp interpolation color space |
115
115
 
116
116
  **Returns** `string[]` — CSS colors.
@@ -123,7 +123,7 @@ Both extremes converge toward an automatically derived **tinted pivot**: the OkL
123
123
  - `colorSpace: "oklch"`: circular path on the hue wheel — more saturated at the center
124
124
 
125
125
  ```ts
126
- // Default symmetric palette (7 classes: 3 + pivot + 3)
126
+ // Default symmetric palette (7 classes: 3 + 3)
127
127
  divergent({ colorA: "#3b4cc0" });
128
128
 
129
129
  // Asymmetric, high contrast, oklch interpolation
@@ -135,8 +135,8 @@ divergent({
135
135
  colorSpace: "oklch",
136
136
  });
137
137
 
138
- // Even count without center class (6 classes)
139
- divergent({ colorA: "steelblue", steps: [3, 3], hasCenterClass: false });
138
+ // With a center class (3 + 1 + 3 classes)
139
+ divergent({ colorA: "steelblue", steps: [3, 3], hasCenterClass: true });
140
140
  ```
141
141
 
142
142
  ### `divergentSequential(options)`
@@ -151,7 +151,7 @@ Accepts the same options as `divergent()` **except** `colorSpace` (interpolation
151
151
  | `colorB` | `string?` | complement | Right-extreme color |
152
152
  | `steps` | `[number, number]` | `[3, 3]` | Classes per side |
153
153
  | `contrast` | `"low" \| "normal" \| "high"` | `"normal"` | Contrast profile |
154
- | `hasCenterClass` | `boolean` | `true` | Add a neutral center class |
154
+ | `hasCenterClass` | `boolean` | `false` | Add a neutral center class |
155
155
 
156
156
  **Returns** `string[]` — CSS colors.
157
157
 
@@ -159,12 +159,12 @@ Accepts the same options as `divergent()` **except** `colorSpace` (interpolation
159
159
  // Rigorously identical to two mirrored `sequential` calls
160
160
  divergentSequential({ colorA: "#3b4cc0" });
161
161
 
162
- // Asymmetric without center class
162
+ // Asymmetric with a center class
163
163
  divergentSequential({
164
164
  colorA: "#d7191c",
165
165
  colorB: "#2b83ba",
166
166
  steps: [5, 2],
167
- hasCenterClass: false,
167
+ hasCenterClass: true,
168
168
  });
169
169
  ```
170
170
 
@@ -203,7 +203,7 @@ Combining: `categorical(8, { ...presets.vif, ...temperature.chaude })`
203
203
 
204
204
  Hue distribution relies on the [Van der Corput sequence](https://en.wikipedia.org/wiki/Van_der_Corput_sequence) in base 2. It always fills the largest gap on the hue wheel, guaranteeing maximum distance between colors regardless of count.
205
205
 
206
- Performance: 10 000 colors in ~4 ms.
206
+ This approach is non iterative. Performance is independent of class count and very fast (~4ms in modern browsers).
207
207
 
208
208
  ### `categoricalPatterns(count, options?)`
209
209
 
@@ -222,6 +222,14 @@ Generates categorical pattern parameters compatible with [@ateliercartographie/m
222
222
 
223
223
  **Returns** `PatternParams[]`
224
224
 
225
+ #### How it works
226
+
227
+ Pattern variation combines three complementary mechanisms so classes stay distinct without looking purely random:
228
+
229
+ - **Shape**: forms are assigned by cycling through `shapes` (`i % shapes.length`) to guarantee balanced reuse when `count` exceeds the shape list.
230
+ - **Scale**: values oscillate between `scaleRange[0]` and `scaleRange[1]` (triangle-wave style), creating contrast while avoiding monotonic growth that could suggest an ordered scale.
231
+ - **Angle**: orientation is sampled with the Van der Corput sequence, but independently **within each shape group**. In practice, each shape gets its own low-discrepancy angle stream, which spreads angles evenly and prevents two identical shapes from clustering around similar directions.
232
+
225
233
  ### `sequentialPatterns(count, options?)`
226
234
 
227
235
  Generates sequential pattern parameters compatible with [@ateliercartographie/motif.js](https://github.com/AtelierCartographie/motif.js).
@@ -250,6 +258,12 @@ Resolves a complex CSS color (`color-mix(…)`, `oklch(from …)`, etc.) to a us
250
258
  - `{ format: "css" }` (default) → `string` as computed by the browser
251
259
  - `{ format: "webgl" }` → `[r, g, b, a]` (0–255)
252
260
 
261
+ #### How it works
262
+
263
+ `resolveColor` delegates parsing and evaluation to the browser color engine: the input is applied to a temporary element/canvas context, then read back as a computed color.
264
+
265
+ This ensures full support for modern CSS color syntax (`oklch`, relative colors, `color-mix()`) without external dependencies and with behavior aligned to real rendering engines. The function then normalizes the result either as a CSS color string (`css`) or as a WebGL-ready numeric tuple (`webgl`).
266
+
253
267
  ### `resolvePalette(colors, options?)`
254
268
 
255
269
  Applies `resolveColor` to an entire array of colors.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=3,T=9,R={low:{start:[85,90],end:[70,50]},normal:{start:[90,97],end:[60,25]},high:{start:[96,99],end:[40,10]}};function k(t){let n=0,e=.5;for(;t>0;)t&1&&(n+=e),t>>=1,e/=2;return n}function x(t,n="normal"){const e=R[n]??R.normal,o=Math.min(1,Math.max(0,(t-P)/(T-P))),i=e.start[0]+o*(e.start[1]-e.start[0]),c=e.end[0]+o*(e.end[1]-e.end[0]);return{start:i,end:c}}function p(t,n){return t[0]+n*(t[1]-t[0])}function S({colorStart:t,colorEnd:n,steps:e=5,contrast:o="normal"}){const i=n??t,{start:c,end:r}=x(e,o),a=o==="low"?1.5:o==="high"?3.5:2.5,l=`oklch(from ${t} ${c.toFixed(1)}% calc(c / ${a}) h)`,s=`oklch(from ${i} ${r.toFixed(1)}% c h)`,f=[];for(let h=0;h<e;h++){const m=e===1?0:h/(e-1)*100;f.push(`color-mix(in oklch, ${l}, ${s} ${m}%)`)}return f}function b({colorA:t,colorB:n,steps:e=[3,3],contrast:o="normal",hasCenterClass:i=!0,colorSpace:c="oklab"}){const[r,a]=e,{start:l,end:s}=x(Math.max(r,a),o),f=`oklch(from ${t} ${s.toFixed(1)}% c h)`,h=n?`oklch(from ${n} ${s.toFixed(1)}% c h)`:`oklch(from ${t} ${s.toFixed(1)}% c calc(h + 180))`,m=`oklch(from ${t} ${l.toFixed(1)}% calc(c / 8) h)`,u=n?`oklch(from ${n} ${l.toFixed(1)}% calc(c / 8) h)`:`oklch(from ${t} ${l.toFixed(1)}% calc(c / 8) calc(h + 180))`,d=`color-mix(in oklab, ${m}, ${u} 50%)`,$=[];for(let g=0;g<r;g++){const F=r===1?0:g/r*100;$.push(`color-mix(in ${c}, ${d} ${F.toFixed(1)}%, ${f})`)}i&&$.push(d);for(let g=a-1;g>=0;g--){const F=a===1?0:g/a*100;$.push(`color-mix(in ${c}, ${d} ${F.toFixed(1)}%, ${h})`)}return $}function w({colorA:t,colorB:n,steps:e=[3,3],contrast:o="normal",hasCenterClass:i=!0}){const[c,r]=e,a=n??`oklch(from ${t} l c calc(h + 180))`,l=S({colorStart:t,steps:c,contrast:o}).reverse(),s=S({colorStart:a,steps:r,contrast:o});if(!i)return[...l,...s];const{start:f}=x(c,o),{start:h}=x(r,o),m=`oklch(from ${t} ${f.toFixed(1)}% 0.02 h)`,u=n?`oklch(from ${n} ${h.toFixed(1)}% 0.02 h)`:`oklch(from ${t} ${h.toFixed(1)}% 0.02 calc(h + 180))`,d=`color-mix(in oklab, ${m}, ${u} 50%)`;return[...l,d,...s]}const A=[.8,.2,.5],q=[.2,.8,.5];function _(t,{hueRange:n=[0,360],hueOffset:e=0,chromaRange:o=[.15,.25],lightnessRange:i=[.5,.75]}={}){const c=[];for(let r=0;r<t;r++){const a=r===0?0:k(r),l=(p(n,a)+e)%360,s=p(i,A[r%3]),f=p(o,q[r%3]);c.push(`oklch(${Math.round(s*100)}% ${f.toFixed(3)} ${l.toFixed(2)})`)}return c}const E=[0,1,.5];function I(t,{shapes:n=["line","circle","plaid","triangle"],angleRange:e=[0,180],scaleRange:o=[2,8],size:i=25,fill:c="#000000",background:r="#ffffff",patchSize:a=!0,vdcOffset:l=0,colorize:s=!1}={}){const f=n.length,h=s?_(t,typeof s=="object"?s:void 0):void 0;return Array.from({length:t},(m,u)=>{const d=u%f,$=Math.floor(u/f),g=n[d],F=d*17,O=k($+l+F),y=Math.round(p(e,O)),L=+p(o,E[u%3]).toFixed(2),M=h?.[u]??c;return{type:g,angle:y,scale:L,size:i,fill:M,background:r,patchSize:a}})}function D(t,{shape:n="line",sizeRange:e=[10,90],angle:o=45,scale:i=3,contrast:c="normal",fill:r="#000000",background:a="#ffffff",patchSize:l=!0}={}){const{start:s,end:f}=x(t,c);return Array.from({length:t},(h,m)=>{const u=t<=1?0:m/(t-1),d=(s+u*(f-s))/100,$=p(e,1-d);return{type:n,angle:o,scale:i,size:Math.round(+$),fill:r,background:a,patchSize:l}})}let v=null;function N(){if(!v&&(v=new OffscreenCanvas(1,1).getContext("2d",{willReadFrequently:!0}),!v))throw new Error("Could not create an OffscreenCanvas 2D context");return v}function C(t,{format:n="css"}={}){const e=N();if(e.fillStyle=t,n==="css")return e.fillStyle;e.clearRect(0,0,1,1),e.fillRect(0,0,1,1);const{data:o}=e.getImageData(0,0,1,1);return[o[0],o[1],o[2],o[3]]}function j(t,{format:n="css"}={}){return n==="webgl"?t.map(e=>C(e,{format:"webgl"})):t.map(e=>C(e,{format:"css"}))}const z={vif:{hueRange:[0,360],chromaRange:[.15,.22],lightnessRange:[.6,.8]},pastel:{hueRange:[0,360],chromaRange:[.05,.1],lightnessRange:[.8,.9]},sepia:{hueRange:[50,90],chromaRange:[.02,.05],lightnessRange:[.6,.9]}},B={mixte:{hueRange:[0,360]},chaude:{hueRange:[300,480]},froide:{hueRange:[120,300]}};exports.CONTRAST_PROFILES=R;exports.categorical=_;exports.categoricalPatterns=I;exports.divergent=b;exports.divergentSequential=w;exports.getAdaptiveBounds=x;exports.presets=z;exports.resolveColor=C;exports.resolvePalette=j;exports.sequential=S;exports.sequentialPatterns=D;exports.temperature=B;exports.vanDerCorput=k;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=3,T=9,R={low:{start:[85,90],end:[70,50]},normal:{start:[90,97],end:[60,25]},high:{start:[96,99],end:[40,10]}};function k(t){let n=0,e=.5;for(;t>0;)t&1&&(n+=e),t>>=1,e/=2;return n}function x(t,n="normal"){const e=R[n]??R.normal,o=Math.min(1,Math.max(0,(t-P)/(T-P))),i=e.start[0]+o*(e.start[1]-e.start[0]),c=e.end[0]+o*(e.end[1]-e.end[0]);return{start:i,end:c}}function p(t,n){return t[0]+n*(t[1]-t[0])}function S({colorStart:t,colorEnd:n,steps:e=5,contrast:o="normal"}){const i=n??t,{start:c,end:s}=x(e,o),a=o==="low"?1.5:o==="high"?3.5:2.5,l=`oklch(from ${t} ${c.toFixed(1)}% calc(c / ${a}) h)`,r=`oklch(from ${i} ${s.toFixed(1)}% c h)`,f=[];for(let h=0;h<e;h++){const m=e===1?0:h/(e-1)*100;f.push(`color-mix(in oklch, ${l}, ${r} ${m}%)`)}return f}function b({colorA:t,colorB:n,steps:e=[3,3],contrast:o="normal",hasCenterClass:i=!1,colorSpace:c="oklab"}){const[s,a]=e,{start:l,end:r}=x(Math.max(s,a),o),f=`oklch(from ${t} ${r.toFixed(1)}% c h)`,h=n?`oklch(from ${n} ${r.toFixed(1)}% c h)`:`oklch(from ${t} ${r.toFixed(1)}% c calc(h + 180))`,m=`oklch(from ${t} ${l.toFixed(1)}% calc(c / 8) h)`,u=n?`oklch(from ${n} ${l.toFixed(1)}% calc(c / 8) h)`:`oklch(from ${t} ${l.toFixed(1)}% calc(c / 8) calc(h + 180))`,d=`color-mix(in oklab, ${m}, ${u} 50%)`,$=[];for(let g=0;g<s;g++){const F=s===1?0:g/s*100;$.push(`color-mix(in ${c}, ${d} ${F.toFixed(1)}%, ${f})`)}i&&$.push(d);for(let g=a-1;g>=0;g--){const F=a===1?0:g/a*100;$.push(`color-mix(in ${c}, ${d} ${F.toFixed(1)}%, ${h})`)}return $}function w({colorA:t,colorB:n,steps:e=[3,3],contrast:o="normal",hasCenterClass:i=!1}){const[c,s]=e,a=n??`oklch(from ${t} l c calc(h + 180))`,l=S({colorStart:t,steps:c,contrast:o}).reverse(),r=S({colorStart:a,steps:s,contrast:o});if(!i)return[...l,...r];const{start:f}=x(c,o),{start:h}=x(s,o),m=`oklch(from ${t} ${f.toFixed(1)}% 0.02 h)`,u=n?`oklch(from ${n} ${h.toFixed(1)}% 0.02 h)`:`oklch(from ${t} ${h.toFixed(1)}% 0.02 calc(h + 180))`,d=`color-mix(in oklab, ${m}, ${u} 50%)`;return[...l,d,...r]}const A=[.8,.2,.5],q=[.2,.8,.5];function _(t,{hueRange:n=[0,360],hueOffset:e=0,chromaRange:o=[.15,.25],lightnessRange:i=[.5,.75]}={}){const c=[];for(let s=0;s<t;s++){const a=s===0?0:k(s),l=(p(n,a)+e)%360,r=p(i,A[s%3]),f=p(o,q[s%3]);c.push(`oklch(${Math.round(r*100)}% ${f.toFixed(3)} ${l.toFixed(2)})`)}return c}const E=[0,1,.5];function I(t,{shapes:n=["line","circle","plaid","triangle"],angleRange:e=[0,180],scaleRange:o=[2,8],size:i=25,fill:c="#000000",background:s="#ffffff",patchSize:a=!0,vdcOffset:l=0,colorize:r=!1}={}){const f=n.length,h=r?_(t,typeof r=="object"?r:void 0):void 0;return Array.from({length:t},(m,u)=>{const d=u%f,$=Math.floor(u/f),g=n[d],F=d*17,O=k($+l+F),y=Math.round(p(e,O)),L=+p(o,E[u%3]).toFixed(2),M=h?.[u]??c;return{type:g,angle:y,scale:L,size:i,fill:M,background:s,patchSize:a}})}function D(t,{shape:n="line",sizeRange:e=[10,90],angle:o=45,scale:i=3,contrast:c="normal",fill:s="#000000",background:a="#ffffff",patchSize:l=!0}={}){const{start:r,end:f}=x(t,c);return Array.from({length:t},(h,m)=>{const u=t<=1?0:m/(t-1),d=(r+u*(f-r))/100,$=p(e,1-d);return{type:n,angle:o,scale:i,size:Math.round(+$),fill:s,background:a,patchSize:l}})}let v=null;function N(){if(!v&&(v=new OffscreenCanvas(1,1).getContext("2d",{willReadFrequently:!0}),!v))throw new Error("Could not create an OffscreenCanvas 2D context");return v}function C(t,{format:n="css"}={}){const e=N();if(e.fillStyle=t,n==="css")return e.fillStyle;e.clearRect(0,0,1,1),e.fillRect(0,0,1,1);const{data:o}=e.getImageData(0,0,1,1);return[o[0],o[1],o[2],o[3]]}function j(t,{format:n="css"}={}){return n==="webgl"?t.map(e=>C(e,{format:"webgl"})):t.map(e=>C(e,{format:"css"}))}const z={vif:{hueRange:[0,360],chromaRange:[.15,.22],lightnessRange:[.6,.8]},pastel:{hueRange:[0,360],chromaRange:[.05,.1],lightnessRange:[.8,.9]},sepia:{hueRange:[50,90],chromaRange:[.02,.05],lightnessRange:[.6,.9]}},B={mixte:{hueRange:[0,360]},chaude:{hueRange:[300,480]},froide:{hueRange:[120,300]}};exports.CONTRAST_PROFILES=R;exports.categorical=_;exports.categoricalPatterns=I;exports.divergent=b;exports.divergentSequential=w;exports.getAdaptiveBounds=x;exports.presets=z;exports.resolveColor=C;exports.resolvePalette=j;exports.sequential=S;exports.sequentialPatterns=D;exports.temperature=B;exports.vanDerCorput=k;
@@ -43,7 +43,7 @@ function b({
43
43
  colorB: n,
44
44
  steps: e = [3, 3],
45
45
  contrast: o = "normal",
46
- hasCenterClass: i = !0,
46
+ hasCenterClass: i = !1,
47
47
  colorSpace: r = "oklab"
48
48
  }) {
49
49
  const [c, a] = e, { start: l, end: s } = F(
@@ -66,7 +66,7 @@ function A({
66
66
  colorB: n,
67
67
  steps: e = [3, 3],
68
68
  contrast: o = "normal",
69
- hasCenterClass: i = !0
69
+ hasCenterClass: i = !1
70
70
  }) {
71
71
  const [r, c] = e, a = n ?? `oklch(from ${t} l c calc(h + 180))`, l = k({ colorStart: t, steps: r, contrast: o }).reverse(), s = k({ colorStart: a, steps: c, contrast: o });
72
72
  if (!i)
@@ -40,7 +40,7 @@ export interface DivergentPaletteOptions {
40
40
  steps?: [number, number];
41
41
  /** Contrast profile (default: "normal") */
42
42
  contrast?: ContrastMode;
43
- /** Add a neutral center class (default: true) */
43
+ /** Add a neutral center class (default: false) */
44
44
  hasCenterClass?: boolean;
45
45
  /**
46
46
  * Color space for interpolating each ramp (default: "oklab").
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ateliercartographie/ok-palette",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Générateur de palettes de couleurs séquentielles, divergentes et catégorielles pour la dataviz et la cartographie",
5
5
  "type": "module",
6
6
  "main": "./dist/ok-palette.cjs",