@studiotoolkit/color-from-image 1.0.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/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/ColorFromImage.d.ts +18 -0
- package/dist/ColorFromImage.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +215 -0
- package/dist/style.css +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Studio Toolkit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Color From Image
|
|
2
|
+
|
|
3
|
+
React component for extracting dominant colors from images using advanced color analysis algorithms.
|
|
4
|
+
|
|
5
|
+
**Play around and Generate component code here:**
|
|
6
|
+
https://studiotoolkit.github.io/build-color-from-image
|
|
7
|
+
|
|
8
|
+
**Complete Demo:**
|
|
9
|
+
https://studiotoolkit.github.io/demo-all
|
|
10
|
+
|
|
11
|
+
**Complete functionality:**
|
|
12
|
+
https://studiotoolkit.github.io/color-from-image-help
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
<table>
|
|
17
|
+
<tr>
|
|
18
|
+
<td align="center"><img src="assets/Empty-Canvas.png" width="200"/><br/>Empty Canvas</td>
|
|
19
|
+
</tr>
|
|
20
|
+
<tr>
|
|
21
|
+
<td align="center"><img src="assets/Image-Pasted.png" width="200"/><br/>Image Pasted</td>
|
|
22
|
+
</tr>
|
|
23
|
+
<tr>
|
|
24
|
+
<td align="center"><img src="assets/Settings.png" width="200"/><br/>Parameters</td>
|
|
25
|
+
</tr>
|
|
26
|
+
</table>
|
|
27
|
+
|
|
28
|
+
- Extract dominant colors from any image using OKLab color space and K-Means++ clustering
|
|
29
|
+
- Drag and drop or paste image support (Ctrl+V)
|
|
30
|
+
- Customizable canvas dimensions (`width`, `height`)
|
|
31
|
+
- Border styling options (`borderColor`, `borderThickness`)
|
|
32
|
+
- Load from URL (`imageUrl`) or file object (`imageFile`)
|
|
33
|
+
- Programmatic clear with `clearTrigger`
|
|
34
|
+
- CORS-aware image loading with fallback
|
|
35
|
+
- Color extraction callback (`onColorsExtracted`)
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Using pnpm
|
|
41
|
+
pnpm add @studiotoolkit/color-from-image
|
|
42
|
+
|
|
43
|
+
# Using npm
|
|
44
|
+
npm install @studiotoolkit/color-from-image
|
|
45
|
+
|
|
46
|
+
# Using yarn
|
|
47
|
+
yarn add @studiotoolkit/color-from-image
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**React Version:** `^18.0.0` or higher
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Basic Usage
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { ColorFromImage } from '@studiotoolkit/color-from-image';
|
|
58
|
+
import '@studiotoolkit/color-from-image/style.css';
|
|
59
|
+
|
|
60
|
+
function App() {
|
|
61
|
+
const handleColorsExtracted = (colors) => {
|
|
62
|
+
console.log('Extracted colors:', colors);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return <ColorFromImage onColorsExtracted={handleColorsExtracted} />;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Sample Output:**
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"palette": [
|
|
74
|
+
"#627bb8",
|
|
75
|
+
"#53689e",
|
|
76
|
+
"#4c597d",
|
|
77
|
+
"#6c6d67",
|
|
78
|
+
"#6b3d18",
|
|
79
|
+
"#657092",
|
|
80
|
+
"#43311b",
|
|
81
|
+
"#494b4e",
|
|
82
|
+
"#81889e",
|
|
83
|
+
"#965528",
|
|
84
|
+
"#bc773f",
|
|
85
|
+
"#221b15",
|
|
86
|
+
"#808748",
|
|
87
|
+
"#615d2a",
|
|
88
|
+
"#cdab71",
|
|
89
|
+
"#f1d85d"
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Steps to Use
|
|
95
|
+
|
|
96
|
+
1. Import the component and its styles
|
|
97
|
+
2. Add the `ColorFromImage` component to your app
|
|
98
|
+
3. Paste an image or drag and drop onto the canvas
|
|
99
|
+
4. The component will automatically extract dominant colors
|
|
100
|
+
5. Use the `onColorsExtracted` callback to receive the color palette
|
|
101
|
+
|
|
102
|
+
## Contributors
|
|
103
|
+
|
|
104
|
+
[StudioToolkit](https://github.com/studiotoolkit)
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
Licensed under the MIT License.
|
|
109
|
+
|
|
110
|
+
## Keywords
|
|
111
|
+
|
|
112
|
+
color-extraction, image-colors, dominant-colors, color-analysis, image-processing, color-palette, react-component, design-tools
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import './ColorFromImage.css';
|
|
3
|
+
export interface ColorEntry {
|
|
4
|
+
palette: string[];
|
|
5
|
+
}
|
|
6
|
+
interface ColorFromImageProps {
|
|
7
|
+
width?: number;
|
|
8
|
+
height?: number;
|
|
9
|
+
borderColor?: string;
|
|
10
|
+
borderThickness?: number;
|
|
11
|
+
onColorsExtracted?: (colors: ColorEntry) => void;
|
|
12
|
+
imageUrl?: string;
|
|
13
|
+
imageFile?: File | null;
|
|
14
|
+
clearTrigger?: number;
|
|
15
|
+
}
|
|
16
|
+
declare const ColorFromImage: React.FC<ColorFromImageProps>;
|
|
17
|
+
export default ColorFromImage;
|
|
18
|
+
//# sourceMappingURL=ColorFromImage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ColorFromImage.d.ts","sourceRoot":"","sources":["../src/ColorFromImage.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAuD,MAAM,OAAO,CAAC;AAC5E,OAAO,sBAAsB,CAAC;AAE9B,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,UAAU,mBAAmB;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,CAAC;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAoOD,QAAA,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAgKjD,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC7D,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("react/jsx-runtime"),v=require("react"),I=t=>t<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4),j=t=>t<=.0031308?12.92*t:1.055*Math.pow(t,1/2.4)-.055,R=t=>Math.max(0,Math.min(1,t)),$=({r:t,g:r,b:p})=>{const s=I(t),m=I(r),c=I(p),u=Math.cbrt(.4122214708*s+.5363325363*m+.0514459929*c),h=Math.cbrt(.2119034982*s+.6806995451*m+.1073969566*c),b=Math.cbrt(.0883024619*s+.2817188376*m+.6299787005*c);return{L:.2104542553*u+.793617785*h-.0040720468*b,a:1.9779984951*u-2.428592205*h+.4505937099*b,b:.0259040371*u+.7827717662*h-.808675766*b}},k=({L:t,a:r,b:p})=>{const s=t+.3963377774*r+.2158037573*p,m=t-.1055613458*r-.0638541728*p,c=t-.0894841775*r-1.291485548*p,u=s*s*s,h=m*m*m,b=c*c*c;return{r:R(j(4.0767416621*u-3.3077115913*h+.2309699292*b)),g:R(j(-1.2684380046*u+2.6097574011*h-.3413193965*b)),b:R(j(-.0041960863*u-.7034186147*h+1.707614701*b))}},C=(t,r)=>(t.L-r.L)**2+(t.a-r.a)**2+(t.b-r.b)**2,O=t=>Math.sqrt(t.a*t.a+t.b*t.b),A=({r:t,g:r,b:p})=>{const s=m=>Math.round(m*255).toString(16).padStart(2,"0");return`#${s(t)}${s(r)}${s(p)}`},F=(t,r,p=20)=>{const s=[],m=Math.max(1,Math.floor(t.length/4/4e3));for(let n=0;n<t.length;n+=4*m){if(t[n+3]<128)continue;const l=$({r:t[n]/255,g:t[n+1]/255,b:t[n+2]/255}),f=O(l),e=f<.05?.2:f<.15?.6:1;Math.random()>e||s.push(l)}if(s.length===0)return[];const c=s.length,u=[];u.push(s[Math.floor(Math.random()*c)]);for(let n=1;n<r;n++){const l=s.map(d=>Math.min(...u.map(M=>C(d,M)))),f=l.reduce((d,M)=>d+M,0);let e=Math.random()*f,g=0;for(let d=0;d<c;d++)if(e-=l[d],e<=0){g=d;break}u.push({...s[g]})}const h=new Int32Array(c);for(let n=0;n<p;n++){let l=!1;for(let e=0;e<c;e++){let g=0,d=1/0;for(let M=0;M<r;M++){const D=C(s[e],u[M]);D<d&&(d=D,g=M)}h[e]!==g&&(h[e]=g,l=!0)}if(!l)break;const f=Array.from({length:r},()=>({L:0,a:0,b:0,count:0}));for(let e=0;e<c;e++){const g=h[e];f[g].L+=s[e].L,f[g].a+=s[e].a,f[g].b+=s[e].b,f[g].count++}for(let e=0;e<r;e++)f[e].count>0&&(u[e]={L:f[e].L/f[e].count,a:f[e].a/f[e].count,b:f[e].b/f[e].count})}const b=new Array(r).fill(0);for(let n=0;n<c;n++)b[h[n]]++;return u.map((n,l)=>({hex:A(k(n)),count:b[l],total:c,score:b[l]*(1+3*O(n))})).filter(n=>n.count>0).sort((n,l)=>l.score-n.score)},S=(t,r=16)=>{const p=t.naturalWidth,s=t.naturalHeight;if(!p||!s)return{palette:[]};const m=document.createElement("canvas");m.width=p,m.height=s;const c=m.getContext("2d");if(!c)return{palette:[]};c.drawImage(t,0,0);let u;try{u=c.getImageData(0,0,p,s).data}catch{return{palette:[]}}return{palette:F(u,r).map(({hex:h})=>h)}},N=({width:t=200,height:r=200,borderColor:p="#cccccc",borderThickness:s=1,onColorsExtracted:m,imageUrl:c,imageFile:u,clearTrigger:h})=>{const b=v.useRef(null),n=v.useRef(()=>{}),l=v.useRef(m),[f,e]=v.useState(null),[g,d]=v.useState(""),M=o=>{const a=b.current;if(!a)return;const i=a.getContext("2d");if(!i)return;i.clearRect(0,0,t,r);const x=Math.min(t/o.width,r/o.height),y=o.width*x,L=o.height*x;i.drawImage(o,(t-y)/2,(r-L)/2,y,L)};v.useLayoutEffect(()=>{n.current=M}),v.useLayoutEffect(()=>{l.current=m}),v.useEffect(()=>{f&&n.current(f)},[t,r,f]);const D=o=>{const a=new FileReader;a.onload=()=>{const i=new Image;i.onload=()=>{var y;e(i),d(""),n.current(i);const x=S(i);x.palette.length>0&&((y=l.current)==null||y.call(l,x))},i.src=a.result},a.readAsDataURL(o)};v.useEffect(()=>{if(!c)return;d("");const o=new Image;o.crossOrigin="anonymous",o.src=c,o.onload=()=>{var i;e(o),n.current(o);const a=S(o);a.palette.length>0?(i=l.current)==null||i.call(l,a):d("Image loaded but color extraction failed.")},o.onerror=()=>{const a=new Image;a.src=c,a.onload=()=>{e(a),n.current(a),d("Displayed but CORS blocked color extraction. Upload the file directly.")},a.onerror=()=>d("Failed to load image. Check the URL and try again.")}},[c]),v.useEffect(()=>{u&&(d(""),D(u))},[u]),v.useEffect(()=>{var a,i;if(!h)return;const o=(a=b.current)==null?void 0:a.getContext("2d");o==null||o.clearRect(0,0,t,r),e(null),d(""),(i=l.current)==null||i.call(l,{palette:[]})},[h,t,r]),v.useEffect(()=>{const o=a=>{var x;const i=(x=a.clipboardData)==null?void 0:x.items;if(i){for(let y=0;y<i.length;y++)if(i[y].type.startsWith("image")){const L=i[y].getAsFile();if(L){D(L);return}}}};return window.addEventListener("paste",o),()=>window.removeEventListener("paste",o)},[]);const E=o=>o.preventDefault(),T=o=>{var i;o.preventDefault();const a=(i=o.dataTransfer.files)==null?void 0:i[0];a!=null&&a.type.startsWith("image/")&&D(a)};return w.jsxs("div",{className:"cfi-container",children:[w.jsxs("div",{className:"cfi-canvas-wrapper",style:{borderColor:p,borderWidth:`${s}px`},onDragOver:E,onDrop:T,children:[w.jsx("canvas",{ref:b,width:t,height:r,className:"cfi-canvas",style:{width:`${t}px`,height:`${r}px`}}),!f&&w.jsxs("div",{className:"cfi-canvas-overlay",children:[w.jsx("span",{children:"Drag & Drop"}),w.jsx("span",{children:"or Ctrl+V / Paste"})]})]}),g&&w.jsx("p",{className:"cfi-url-error",children:g})]})};exports.ColorFromImage=N;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { jsxs as I, jsx as L } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as C, useState as A, useLayoutEffect as N, useEffect as x } from "react";
|
|
3
|
+
const O = (t) => t <= 0.04045 ? t / 12.92 : Math.pow((t + 0.055) / 1.055, 2.4), $ = (t) => t <= 31308e-7 ? 12.92 * t : 1.055 * Math.pow(t, 1 / 2.4) - 0.055, k = (t) => Math.max(0, Math.min(1, t)), j = ({ r: t, g: r, b: p }) => {
|
|
4
|
+
const o = O(t), m = O(r), c = O(p), u = Math.cbrt(0.4122214708 * o + 0.5363325363 * m + 0.0514459929 * c), h = Math.cbrt(0.2119034982 * o + 0.6806995451 * m + 0.1073969566 * c), b = Math.cbrt(0.0883024619 * o + 0.2817188376 * m + 0.6299787005 * c);
|
|
5
|
+
return {
|
|
6
|
+
L: 0.2104542553 * u + 0.793617785 * h - 0.0040720468 * b,
|
|
7
|
+
a: 1.9779984951 * u - 2.428592205 * h + 0.4505937099 * b,
|
|
8
|
+
b: 0.0259040371 * u + 0.7827717662 * h - 0.808675766 * b
|
|
9
|
+
};
|
|
10
|
+
}, z = ({ L: t, a: r, b: p }) => {
|
|
11
|
+
const o = t + 0.3963377774 * r + 0.2158037573 * p, m = t - 0.1055613458 * r - 0.0638541728 * p, c = t - 0.0894841775 * r - 1.291485548 * p, u = o * o * o, h = m * m * m, b = c * c * c;
|
|
12
|
+
return {
|
|
13
|
+
r: k($(4.0767416621 * u - 3.3077115913 * h + 0.2309699292 * b)),
|
|
14
|
+
g: k($(-1.2684380046 * u + 2.6097574011 * h - 0.3413193965 * b)),
|
|
15
|
+
b: k($(-0.0041960863 * u - 0.7034186147 * h + 1.707614701 * b))
|
|
16
|
+
};
|
|
17
|
+
}, R = (t, r) => (t.L - r.L) ** 2 + (t.a - r.a) ** 2 + (t.b - r.b) ** 2, T = (t) => Math.sqrt(t.a * t.a + t.b * t.b), G = ({ r: t, g: r, b: p }) => {
|
|
18
|
+
const o = (m) => Math.round(m * 255).toString(16).padStart(2, "0");
|
|
19
|
+
return `#${o(t)}${o(r)}${o(p)}`;
|
|
20
|
+
}, H = (t, r, p = 20) => {
|
|
21
|
+
const o = [], m = Math.max(1, Math.floor(t.length / 4 / 4e3));
|
|
22
|
+
for (let n = 0; n < t.length; n += 4 * m) {
|
|
23
|
+
if (t[n + 3] < 128) continue;
|
|
24
|
+
const l = j({
|
|
25
|
+
r: t[n] / 255,
|
|
26
|
+
g: t[n + 1] / 255,
|
|
27
|
+
b: t[n + 2] / 255
|
|
28
|
+
}), f = T(l), e = f < 0.05 ? 0.2 : f < 0.15 ? 0.6 : 1;
|
|
29
|
+
Math.random() > e || o.push(l);
|
|
30
|
+
}
|
|
31
|
+
if (o.length === 0) return [];
|
|
32
|
+
const c = o.length, u = [];
|
|
33
|
+
u.push(o[Math.floor(Math.random() * c)]);
|
|
34
|
+
for (let n = 1; n < r; n++) {
|
|
35
|
+
const l = o.map((d) => Math.min(...u.map((v) => R(d, v)))), f = l.reduce((d, v) => d + v, 0);
|
|
36
|
+
let e = Math.random() * f, g = 0;
|
|
37
|
+
for (let d = 0; d < c; d++)
|
|
38
|
+
if (e -= l[d], e <= 0) {
|
|
39
|
+
g = d;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
u.push({ ...o[g] });
|
|
43
|
+
}
|
|
44
|
+
const h = new Int32Array(c);
|
|
45
|
+
for (let n = 0; n < p; n++) {
|
|
46
|
+
let l = !1;
|
|
47
|
+
for (let e = 0; e < c; e++) {
|
|
48
|
+
let g = 0, d = 1 / 0;
|
|
49
|
+
for (let v = 0; v < r; v++) {
|
|
50
|
+
const y = R(o[e], u[v]);
|
|
51
|
+
y < d && (d = y, g = v);
|
|
52
|
+
}
|
|
53
|
+
h[e] !== g && (h[e] = g, l = !0);
|
|
54
|
+
}
|
|
55
|
+
if (!l) break;
|
|
56
|
+
const f = Array.from({ length: r }, () => ({
|
|
57
|
+
L: 0,
|
|
58
|
+
a: 0,
|
|
59
|
+
b: 0,
|
|
60
|
+
count: 0
|
|
61
|
+
}));
|
|
62
|
+
for (let e = 0; e < c; e++) {
|
|
63
|
+
const g = h[e];
|
|
64
|
+
f[g].L += o[e].L, f[g].a += o[e].a, f[g].b += o[e].b, f[g].count++;
|
|
65
|
+
}
|
|
66
|
+
for (let e = 0; e < r; e++)
|
|
67
|
+
f[e].count > 0 && (u[e] = {
|
|
68
|
+
L: f[e].L / f[e].count,
|
|
69
|
+
a: f[e].a / f[e].count,
|
|
70
|
+
b: f[e].b / f[e].count
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const b = new Array(r).fill(0);
|
|
74
|
+
for (let n = 0; n < c; n++) b[h[n]]++;
|
|
75
|
+
return u.map((n, l) => ({
|
|
76
|
+
hex: G(z(n)),
|
|
77
|
+
count: b[l],
|
|
78
|
+
total: c,
|
|
79
|
+
// Vivid centroids score higher than their raw pixel count alone
|
|
80
|
+
score: b[l] * (1 + 3 * T(n))
|
|
81
|
+
})).filter((n) => n.count > 0).sort((n, l) => l.score - n.score);
|
|
82
|
+
}, F = (t, r = 16) => {
|
|
83
|
+
const p = t.naturalWidth, o = t.naturalHeight;
|
|
84
|
+
if (!p || !o) return { palette: [] };
|
|
85
|
+
const m = document.createElement("canvas");
|
|
86
|
+
m.width = p, m.height = o;
|
|
87
|
+
const c = m.getContext("2d");
|
|
88
|
+
if (!c) return { palette: [] };
|
|
89
|
+
c.drawImage(t, 0, 0);
|
|
90
|
+
let u;
|
|
91
|
+
try {
|
|
92
|
+
u = c.getImageData(0, 0, p, o).data;
|
|
93
|
+
} catch {
|
|
94
|
+
return { palette: [] };
|
|
95
|
+
}
|
|
96
|
+
return { palette: H(u, r).map(({ hex: h }) => h) };
|
|
97
|
+
}, B = ({
|
|
98
|
+
width: t = 200,
|
|
99
|
+
height: r = 200,
|
|
100
|
+
borderColor: p = "#cccccc",
|
|
101
|
+
borderThickness: o = 1,
|
|
102
|
+
onColorsExtracted: m,
|
|
103
|
+
imageUrl: c,
|
|
104
|
+
imageFile: u,
|
|
105
|
+
clearTrigger: h
|
|
106
|
+
}) => {
|
|
107
|
+
const b = C(null), n = C(() => {
|
|
108
|
+
}), l = C(m), [f, e] = A(null), [g, d] = A(""), v = (a) => {
|
|
109
|
+
const s = b.current;
|
|
110
|
+
if (!s) return;
|
|
111
|
+
const i = s.getContext("2d");
|
|
112
|
+
if (!i) return;
|
|
113
|
+
i.clearRect(0, 0, t, r);
|
|
114
|
+
const w = Math.min(t / a.width, r / a.height), M = a.width * w, D = a.height * w;
|
|
115
|
+
i.drawImage(a, (t - M) / 2, (r - D) / 2, M, D);
|
|
116
|
+
};
|
|
117
|
+
N(() => {
|
|
118
|
+
n.current = v;
|
|
119
|
+
}), N(() => {
|
|
120
|
+
l.current = m;
|
|
121
|
+
}), x(() => {
|
|
122
|
+
f && n.current(f);
|
|
123
|
+
}, [t, r, f]);
|
|
124
|
+
const y = (a) => {
|
|
125
|
+
const s = new FileReader();
|
|
126
|
+
s.onload = () => {
|
|
127
|
+
const i = new Image();
|
|
128
|
+
i.onload = () => {
|
|
129
|
+
var M;
|
|
130
|
+
e(i), d(""), n.current(i);
|
|
131
|
+
const w = F(i);
|
|
132
|
+
w.palette.length > 0 && ((M = l.current) == null || M.call(l, w));
|
|
133
|
+
}, i.src = s.result;
|
|
134
|
+
}, s.readAsDataURL(a);
|
|
135
|
+
};
|
|
136
|
+
x(() => {
|
|
137
|
+
if (!c) return;
|
|
138
|
+
d("");
|
|
139
|
+
const a = new Image();
|
|
140
|
+
a.crossOrigin = "anonymous", a.src = c, a.onload = () => {
|
|
141
|
+
var i;
|
|
142
|
+
e(a), n.current(a);
|
|
143
|
+
const s = F(a);
|
|
144
|
+
s.palette.length > 0 ? (i = l.current) == null || i.call(l, s) : d("Image loaded but color extraction failed.");
|
|
145
|
+
}, a.onerror = () => {
|
|
146
|
+
const s = new Image();
|
|
147
|
+
s.src = c, s.onload = () => {
|
|
148
|
+
e(s), n.current(s), d("Displayed but CORS blocked color extraction. Upload the file directly.");
|
|
149
|
+
}, s.onerror = () => d("Failed to load image. Check the URL and try again.");
|
|
150
|
+
};
|
|
151
|
+
}, [c]), x(() => {
|
|
152
|
+
u && (d(""), y(u));
|
|
153
|
+
}, [u]), x(() => {
|
|
154
|
+
var s, i;
|
|
155
|
+
if (!h) return;
|
|
156
|
+
const a = (s = b.current) == null ? void 0 : s.getContext("2d");
|
|
157
|
+
a == null || a.clearRect(0, 0, t, r), e(null), d(""), (i = l.current) == null || i.call(l, { palette: [] });
|
|
158
|
+
}, [h, t, r]), x(() => {
|
|
159
|
+
const a = (s) => {
|
|
160
|
+
var w;
|
|
161
|
+
const i = (w = s.clipboardData) == null ? void 0 : w.items;
|
|
162
|
+
if (i) {
|
|
163
|
+
for (let M = 0; M < i.length; M++)
|
|
164
|
+
if (i[M].type.startsWith("image")) {
|
|
165
|
+
const D = i[M].getAsFile();
|
|
166
|
+
if (D) {
|
|
167
|
+
y(D);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
return window.addEventListener("paste", a), () => window.removeEventListener("paste", a);
|
|
174
|
+
}, []);
|
|
175
|
+
const S = (a) => a.preventDefault(), W = (a) => {
|
|
176
|
+
var i;
|
|
177
|
+
a.preventDefault();
|
|
178
|
+
const s = (i = a.dataTransfer.files) == null ? void 0 : i[0];
|
|
179
|
+
s != null && s.type.startsWith("image/") && y(s);
|
|
180
|
+
};
|
|
181
|
+
return /* @__PURE__ */ I("div", { className: "cfi-container", children: [
|
|
182
|
+
/* @__PURE__ */ I(
|
|
183
|
+
"div",
|
|
184
|
+
{
|
|
185
|
+
className: "cfi-canvas-wrapper",
|
|
186
|
+
style: {
|
|
187
|
+
borderColor: p,
|
|
188
|
+
borderWidth: `${o}px`
|
|
189
|
+
},
|
|
190
|
+
onDragOver: S,
|
|
191
|
+
onDrop: W,
|
|
192
|
+
children: [
|
|
193
|
+
/* @__PURE__ */ L(
|
|
194
|
+
"canvas",
|
|
195
|
+
{
|
|
196
|
+
ref: b,
|
|
197
|
+
width: t,
|
|
198
|
+
height: r,
|
|
199
|
+
className: "cfi-canvas",
|
|
200
|
+
style: { width: `${t}px`, height: `${r}px` }
|
|
201
|
+
}
|
|
202
|
+
),
|
|
203
|
+
!f && /* @__PURE__ */ I("div", { className: "cfi-canvas-overlay", children: [
|
|
204
|
+
/* @__PURE__ */ L("span", { children: "Drag & Drop" }),
|
|
205
|
+
/* @__PURE__ */ L("span", { children: "or Ctrl+V / Paste" })
|
|
206
|
+
] })
|
|
207
|
+
]
|
|
208
|
+
}
|
|
209
|
+
),
|
|
210
|
+
g && /* @__PURE__ */ L("p", { className: "cfi-url-error", children: g })
|
|
211
|
+
] });
|
|
212
|
+
};
|
|
213
|
+
export {
|
|
214
|
+
B as ColorFromImage
|
|
215
|
+
};
|
package/dist/style.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.cfi-container{display:flex;flex-direction:column;align-items:flex-start;gap:8px;background-color:#fff}[data-theme=dark] .cfi-container{background-color:#1a1a1a!important}.cfi-canvas-wrapper{position:relative;display:inline-block;line-height:0;border-style:solid;box-sizing:border-box}[data-theme=dark] .cfi-canvas-wrapper{border-color:#444}.cfi-canvas{display:block}.cfi-canvas-overlay{position:absolute;top:0;right:0;bottom:0;left:0;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:4px;pointer-events:none;background:#f8f8f8d9;line-height:normal}[data-theme=dark] .cfi-canvas-overlay{background:#1a1a1ad9}.cfi-canvas-overlay span{font-size:14px;color:#888;font-family:inherit}[data-theme=dark] .cfi-canvas-overlay span{color:#aaa}.cfi-canvas-overlay span:first-child{font-size:16px;font-weight:600;color:#555}[data-theme=dark] .cfi-canvas-overlay span:first-child{color:#ccc}.cfi-actions-row{display:flex;align-items:center;gap:8px}.cfi-file-input{font-family:inherit;font-size:13px;flex:1}.cfi-clear-button{padding:5px 12px;background:#e74c3c;color:#fff;border:none;border-radius:4px;cursor:pointer;font-size:13px;font-family:inherit;white-space:nowrap}.cfi-clear-button:hover{background:#c0392b}.cfi-paste-button{padding:5px 12px;background:#2196f3;color:#fff;border:none;border-radius:4px;cursor:pointer;font-size:13px;font-family:inherit;white-space:nowrap}.cfi-paste-button:hover{background:#1976d2}.cfi-url-row{display:flex;align-items:center;gap:6px}.cfi-url-input{padding:5px 8px;border:1px solid #ccc;border-radius:4px;font-size:13px;font-family:inherit}.cfi-load-button{padding:5px 12px;background:#3498db;color:#fff;border:none;border-radius:4px;cursor:pointer;font-size:13px;font-family:inherit}.cfi-load-button:hover{background:#217dbb}.cfi-url-error{margin:0;font-size:12px;color:#c0392b}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@studiotoolkit/color-from-image",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "React component for extracting dominant colors from images using advanced color analysis algorithms",
|
|
6
|
+
"author": "StudioToolkit",
|
|
7
|
+
"homepage": "https://studiotoolkit.github.io/build-color-from-image",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/studiotoolkit/studiotoolkit",
|
|
11
|
+
"directory": "packages/color-from-image"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/studiotoolkit/studiotoolkit/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"color-extraction",
|
|
18
|
+
"image-colors",
|
|
19
|
+
"dominant-colors",
|
|
20
|
+
"color-analysis",
|
|
21
|
+
"image-processing",
|
|
22
|
+
"color-palette",
|
|
23
|
+
"react-component",
|
|
24
|
+
"design-tools",
|
|
25
|
+
"oklab",
|
|
26
|
+
"kmeans",
|
|
27
|
+
"color-quantization"
|
|
28
|
+
],
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"sideEffects": [
|
|
33
|
+
"*.css"
|
|
34
|
+
],
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.mjs",
|
|
39
|
+
"require": "./dist/index.js",
|
|
40
|
+
"default": "./dist/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./style.css": "./dist/ColorFromImage.css",
|
|
43
|
+
"./src/ColorFromImage.css": "./src/ColorFromImage.css",
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"README.md",
|
|
49
|
+
"LICENSE"
|
|
50
|
+
],
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=16.0.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"react": "^18.0.0",
|
|
59
|
+
"react-dom": "^18.0.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/react": "^18.2.0",
|
|
63
|
+
"@types/react-dom": "^18.2.0",
|
|
64
|
+
"@vitejs/plugin-react": "^4.2.0",
|
|
65
|
+
"typescript": "^5.3.3",
|
|
66
|
+
"vite": "^5.0.0"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "npx tsc --project tsconfig.json && vite build",
|
|
70
|
+
"dev": "vite build --watch",
|
|
71
|
+
"clean": "rm -rf dist",
|
|
72
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
73
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest",
|
|
76
|
+
"test:ui": "vitest --ui"
|
|
77
|
+
}
|
|
78
|
+
}
|