@aviala-design/color 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Bytedance, Inc. and its affiliates.
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,89 @@
1
+ # ArcoDesign Color
2
+
3
+ ArcoDesign Color Utils.
4
+
5
+ For a given color, a gradient swatch containing ten colors is generated with an algorithm. This works for both light and dark modes.
6
+
7
+ ## Usage
8
+
9
+ ```bash
10
+ npm i @arco-design/color
11
+ ```
12
+
13
+ ```js
14
+ import { generate, getPresetColors } from '@arco-design/color';
15
+
16
+ console.log(generate('#123456'));
17
+
18
+ console.log(getPresetColors());
19
+ // {
20
+ // red: {...},
21
+ // orangered: {...},
22
+ // orange: {...},
23
+ // gold: {...},
24
+ // yellow: {...},
25
+ // lime: {...},
26
+ // green: {...},
27
+ // cyan: {...},
28
+ // blue: {...},
29
+ // arcoblue: {...},
30
+ // purple: {...},
31
+ // pinkpurple: {...},
32
+ // magenta: {...},
33
+ // gray: {...}
34
+ // }
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### generate(color: string, options: Object);
40
+
41
+ #### options.index {number | 1-10}
42
+
43
+ Index (starting from 1) of the gradient colors to be generated.
44
+
45
+ #### options.list {boolean}
46
+
47
+ Whether to generate color array containing the ten colors.
48
+
49
+ #### options.dark
50
+
51
+ Whether to generate colors for dark mode.
52
+
53
+ #### options.format {'hex' | 'rgb' | 'hsl'}
54
+
55
+ Color format.
56
+ ### getPresetColors {Function}
57
+
58
+ Contains 14 preset sets of colors.
59
+
60
+ * `red`
61
+ * `orangered`
62
+ * `orange`
63
+ * `gold`
64
+ * `yellow`
65
+ * `lime`
66
+ * `green`
67
+ * `cyan`
68
+ * `blue`
69
+ * `arcoblue`
70
+ * `purple`
71
+ * `pinkpurple`
72
+ * `magenta`
73
+ * `gray`
74
+
75
+ ```js
76
+ const { red } = getPresetColors();
77
+
78
+ console.log(red.light);
79
+ console.log(red.dark);
80
+ console.log(red.primary);
81
+ ```
82
+
83
+ ### getRgbStr(color: string)
84
+
85
+ For a given color, get the r, g, b value in string
86
+
87
+ ```js
88
+ getRgbStr('#F53F3F') // 245,63,63
89
+ ```
@@ -0,0 +1,90 @@
1
+ # ArcoDesign Color
2
+
3
+ ArcoDesign Color Utils.
4
+
5
+ 根据给定颜色通过算法生成指定包含十个颜色的梯度色板,也可生成暗色模式下包含十个颜色的色板。
6
+
7
+ ## Usage
8
+
9
+ ```bash
10
+ npm i @arco-design/color
11
+ ```
12
+
13
+ ```js
14
+ import { generate, presetColor } from '@arco-design/color';
15
+
16
+ console.log(generate('#123456'));
17
+
18
+ console.log(presetColor);
19
+ // {
20
+ // red: {...},
21
+ // orangered: {...},
22
+ // orange: {...},
23
+ // gold: {...},
24
+ // yellow: {...},
25
+ // lime: {...},
26
+ // green: {...},
27
+ // cyan: {...},
28
+ // blue: {...},
29
+ // arcoblue: {...},
30
+ // purple: {...},
31
+ // pinkpurple: {...},
32
+ // magenta: {...},
33
+ // gray: {...}
34
+ // }
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### generate(color: string, options: Object);
40
+
41
+ #### options.index {number | 1-10}
42
+
43
+ 生成 10 个梯度色中的第几个颜色。
44
+
45
+ #### options.list {boolean}
46
+
47
+ 生成包含十个颜色的梯度颜色数组。
48
+
49
+ #### options.dark
50
+
51
+ 生成暗色色板的颜色。
52
+
53
+ #### options.format {'hex' | 'rgb' | 'hsl'}
54
+
55
+ 生成颜色的格式。
56
+
57
+ ### getPresetColors {Function}
58
+
59
+ 包含了预设的 14 组颜色,包括一组中性灰。
60
+
61
+ * `red`
62
+ * `orangered`
63
+ * `orange`
64
+ * `gold`
65
+ * `yellow`
66
+ * `lime`
67
+ * `green`
68
+ * `cyan`
69
+ * `blue`
70
+ * `arcoblue`
71
+ * `purple`
72
+ * `pinkpurple`
73
+ * `magenta`
74
+ * `gray`
75
+
76
+ ```js
77
+ const { red } = getPresetColors();
78
+
79
+ console.log(red.light);
80
+ console.log(red.dark);
81
+ console.log(red.primary);
82
+ ```
83
+
84
+ ### getRgbStr(color: string)
85
+
86
+ 获得指定颜色的三通道 r, g, b 字符串。
87
+
88
+ ```js
89
+ getRgbStr('#F53F3F') // 245,63,63
90
+ ```
@@ -0,0 +1,17 @@
1
+ export default generate;
2
+ /**
3
+ * @param {string} color
4
+ * @param {Object} options
5
+ * @param {number} options.index 1 - 10 (default: 6)
6
+ * @param {boolean} options.dark
7
+ * @param {boolean} options.list
8
+ * @param {string} options.format 'hex' | 'rgb' | 'hsl'
9
+ *
10
+ * @return string | string[]
11
+ */
12
+ declare function generate(color: string, options?: {
13
+ index: number;
14
+ dark: boolean;
15
+ list: boolean;
16
+ format: string;
17
+ }): any;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 从图片中提取主色调
3
+ * @param {HTMLImageElement} image - 图片元素
4
+ * @returns {Promise<string>} - 提取的主色调(十六进制格式)
5
+ */
6
+ export function extractColorFromImage(image: HTMLImageElement): Promise<string>;
7
+ /**
8
+ * 从文件中读取图片并提取颜色
9
+ * @param {File} file - 图片文件
10
+ * @returns {Promise<string>} - 提取的主色调(十六进制格式)
11
+ */
12
+ export function extractColorFromFile(file: File): Promise<string>;
@@ -0,0 +1,26 @@
1
+ import { default as generate } from './generate.js';
2
+ import { getRgbStr } from './utils.js';
3
+ import { extractColorFromImage, extractColorFromFile } from './image-color.js';
4
+ export namespace colorList {
5
+ let red: string;
6
+ let orangered: string;
7
+ let orange: string;
8
+ let gold: string;
9
+ let yellow: string;
10
+ let lime: string;
11
+ let green: string;
12
+ let cyan: string;
13
+ let blue: string;
14
+ let arcoblue: string;
15
+ let purple: string;
16
+ let pinkpurple: string;
17
+ let magenta: string;
18
+ }
19
+ export function getPresetColors(): {
20
+ gray: {
21
+ light: string[];
22
+ dark: string[];
23
+ primary: string;
24
+ };
25
+ };
26
+ export { generate, getRgbStr, extractColorFromImage, extractColorFromFile };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ (function(h,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("color")):typeof define=="function"&&define.amd?define(["exports","color"],f):(h=typeof globalThis<"u"?globalThis:h||self,f(h.AvialaColor={},h.Color))})(this,function(h,f){"use strict";function P(t){return f(t).rgb().round().color.join(",")}const E=["hex","rgb","hsl"];function A(t){return!t||E.indexOf(t)<0?"hex":t}function D(t,e){const o=A(e);return o==="hex"?t[o]():t[o]().round().string()}function x(t,e,o){const a=f(t),r=a.hue(),n=a.saturationv(),s=a.value(),c=(l=>l>=60&&l<=240?2.5:l>=0&&l<60||l>300&&l<=360?1.5:2)(r),w=100,p=9,m=100,u=30;function b(l,d){let g;return r>=60&&r<=240?g=l?r-c*d:r+c*d:g=l?r+c*d:r-c*d,g<0?g+=360:g>=360&&(g-=360),Math.round(g)}function z(l,d){let g;if(l)g=n<=p?n:n-(n-p)/5.5*Math.pow(d,1.05);else{const O=Math.min(w,n+30);g=n+(O-n)/4.2*Math.pow(d,.95)}return Math.max(0,Math.min(100,g))}function H(l,d){return l?Math.min(m,s+(m-s)/5.2*Math.pow(d,.9)):s<=u?s:Math.max(u,s-(s-u)/4.2*Math.pow(d,1.05))}const M=e<6,S=M?6-e:e-6,L=e===6?a:f({h:b(M,S),s:z(M,S),v:H(M,S)});return D(L,o)}function v(t,e,o){const a=f(x(t,10-e+1)),r=f(t),n=r.hue(),s=r.saturationv(),c=f({h:r.hue(),s:m(6),v:r.value()}).saturationv(),w=Math.ceil((c-9)/4),p=Math.ceil((100-c)/5);function m(b){if(b<6)return c+(6-b)*p;if(b===6){if(n>=0&&n<50)return s-15;if(n>=50&&n<191)return s-20;if(n>=191&&n<=360)return s-15}return c-w*(b-6)}const u=f({h:a.hue(),s:m(e),v:a.value()});return D(u,o)}function y(t,e={}){const{dark:o,list:a,index:r=6,format:n="hex"}=e;if(a){const s=[],i=o?v:x;for(let c=1;c<=10;c++)s.push(i(t,c,n));return s}return o?v(t,r,n):x(t,r,n)}async function C(t){try{const e=await B(t),o=I(e);return q(o)}catch(e){throw console.error("提取图片颜色失败:",e),e}}async function B(t){return new Promise((e,o)=>{try{const a=document.createElement("canvas"),r=a.getContext("2d"),n=Math.min(t.width,100),s=Math.min(t.height,100),i=Math.min(n/t.width,s/t.height);a.width=t.width*i,a.height=t.height*i,r.drawImage(t,0,0,a.width,a.height);const c=r.getImageData(0,0,a.width,a.height);e(c)}catch(a){o(a)}})}function I(t){const e=t.data,o=new Map;for(let r=0;r<e.length;r+=4){const n=e[r],s=e[r+1],i=e[r+2];if(e[r+3]<128)continue;const w=Math.round(n/16)*16,p=Math.round(s/16)*16,m=Math.round(i/16)*16,u=`${w},${p},${m}`;o.has(u)?o.set(u,o.get(u)+1):o.set(u,1)}const a=[];return o.forEach((r,n)=>{const[s,i,c]=n.split(",").map(Number);a.push({r:s,g:i,b:c,count:r})}),a}function q(t){t.sort((r,n)=>n.count-r.count);const e=t.filter(r=>{const{r:n,g:s,b:i}=r,c=Math.max(n,s,i),w=Math.min(n,s,i),p=c-w,m=c===0?0:p/c,u=c/255;return m>.15&&u>.2&&u<.8}),o=e.length>0?e[0]:t[0];return f({r:o.r,g:o.g,b:o.b}).hex()}function N(t){return new Promise((e,o)=>{if(!t.type.startsWith("image/")){o(new Error("请选择图片文件"));return}const a=new FileReader;a.onload=async r=>{try{const n=new Image;n.onload=async()=>{try{const s=await C(n);e(s)}catch(s){o(s)}},n.onerror=()=>o(new Error("图片加载失败")),n.src=r.target.result}catch(n){o(n)}},a.onerror=()=>o(new Error("文件读取失败")),a.readAsDataURL(t)})}const F={red:"#F53F3F",orangered:"#F77234",orange:"#FF7D00",gold:"#F7BA1E",yellow:"#FADC19",lime:"#9FDB1D",green:"#00B42A",cyan:"#14C9C9",blue:"#3491FA",arcoblue:"#165DFF",purple:"#722ED1",pinkpurple:"#D91AD9",magenta:"#F5319D"};function R(){const t={};return Object.keys(F).forEach(e=>{t[e]={},t[e].light=y(F[e],{list:!0}),t[e].dark=y(F[e],{list:!0,dark:!0}),t[e].primary=F[e]}),t.gray={},t.gray.light=["#f7f8fa","#f2f3f5","#e5e6eb","#c9cdd4","#a9aeb8","#86909c","#6b7785","#4e5969","#272e3b","#1d2129"],t.gray.dark=["#17171a","#2e2e30","#484849","#5f5f60","#78787a","#929293","#ababac","#c5c5c5","#dfdfdf","#f6f6f6"],t.gray.primary=t.gray.light[6],t}h.colorList=F,h.extractColorFromFile=N,h.extractColorFromImage=C,h.generate=y,h.getPresetColors=R,h.getRgbStr=P,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/dist/index.mjs ADDED
@@ -0,0 +1,191 @@
1
+ import m from "color";
2
+ function R(t) {
3
+ return m(t).rgb().round().color.join(",");
4
+ }
5
+ const B = ["hex", "rgb", "hsl"];
6
+ function A(t) {
7
+ return !t || B.indexOf(t) < 0 ? "hex" : t;
8
+ }
9
+ function S(t, n) {
10
+ const o = A(n);
11
+ return o === "hex" ? t[o]() : t[o]().round().string();
12
+ }
13
+ function F(t, n, o) {
14
+ const a = m(t), e = a.hue(), r = a.saturationv(), s = a.value(), c = ((l) => l >= 60 && l <= 240 ? 2.5 : l >= 0 && l < 60 || l > 300 && l <= 360 ? 1.5 : 2)(e), p = 100, d = 9, f = 100, u = 30;
15
+ function w(l, g) {
16
+ let h;
17
+ return e >= 60 && e <= 240 ? h = l ? e - c * g : e + c * g : h = l ? e + c * g : e - c * g, h < 0 ? h += 360 : h >= 360 && (h -= 360), Math.round(h);
18
+ }
19
+ function y(l, g) {
20
+ let h;
21
+ if (l)
22
+ h = r <= d ? r : r - (r - d) / 5.5 * Math.pow(g, 1.05);
23
+ else {
24
+ const E = Math.min(p, r + 30);
25
+ h = r + (E - r) / 4.2 * Math.pow(g, 0.95);
26
+ }
27
+ return Math.max(0, Math.min(100, h));
28
+ }
29
+ function v(l, g) {
30
+ return l ? Math.min(f, s + (f - s) / 5.2 * Math.pow(g, 0.9)) : s <= u ? s : Math.max(u, s - (s - u) / 4.2 * Math.pow(g, 1.05));
31
+ }
32
+ const x = n < 6, b = x ? 6 - n : n - 6, P = n === 6 ? a : m({
33
+ h: w(x, b),
34
+ s: y(x, b),
35
+ v: v(x, b)
36
+ });
37
+ return S(P, o);
38
+ }
39
+ function C(t, n, o) {
40
+ const a = m(F(t, 10 - n + 1)), e = m(t), r = e.hue(), s = e.saturationv(), c = m({
41
+ h: e.hue(),
42
+ s: f(6),
43
+ v: e.value()
44
+ }).saturationv(), p = Math.ceil((c - 9) / 4), d = Math.ceil((100 - c) / 5);
45
+ function f(w) {
46
+ if (w < 6)
47
+ return c + (6 - w) * d;
48
+ if (w === 6) {
49
+ if (r >= 0 && r < 50)
50
+ return s - 15;
51
+ if (r >= 50 && r < 191)
52
+ return s - 20;
53
+ if (r >= 191 && r <= 360)
54
+ return s - 15;
55
+ }
56
+ return c - p * (w - 6);
57
+ }
58
+ const u = m({
59
+ h: a.hue(),
60
+ s: f(n),
61
+ v: a.value()
62
+ });
63
+ return S(u, o);
64
+ }
65
+ function D(t, n = {}) {
66
+ const { dark: o, list: a, index: e = 6, format: r = "hex" } = n;
67
+ if (a) {
68
+ const s = [], i = o ? C : F;
69
+ for (let c = 1; c <= 10; c++)
70
+ s.push(i(t, c, r));
71
+ return s;
72
+ }
73
+ return o ? C(t, e, r) : F(t, e, r);
74
+ }
75
+ async function I(t) {
76
+ try {
77
+ const n = await N(t), o = q(n);
78
+ return z(o);
79
+ } catch (n) {
80
+ throw console.error("提取图片颜色失败:", n), n;
81
+ }
82
+ }
83
+ async function N(t) {
84
+ return new Promise((n, o) => {
85
+ try {
86
+ const a = document.createElement("canvas"), e = a.getContext("2d"), r = Math.min(t.width, 100), s = Math.min(t.height, 100), i = Math.min(r / t.width, s / t.height);
87
+ a.width = t.width * i, a.height = t.height * i, e.drawImage(t, 0, 0, a.width, a.height);
88
+ const c = e.getImageData(0, 0, a.width, a.height);
89
+ n(c);
90
+ } catch (a) {
91
+ o(a);
92
+ }
93
+ });
94
+ }
95
+ function q(t) {
96
+ const n = t.data, o = /* @__PURE__ */ new Map();
97
+ for (let e = 0; e < n.length; e += 4) {
98
+ const r = n[e], s = n[e + 1], i = n[e + 2];
99
+ if (n[e + 3] < 128) continue;
100
+ const p = Math.round(r / 16) * 16, d = Math.round(s / 16) * 16, f = Math.round(i / 16) * 16, u = `${p},${d},${f}`;
101
+ o.has(u) ? o.set(u, o.get(u) + 1) : o.set(u, 1);
102
+ }
103
+ const a = [];
104
+ return o.forEach((e, r) => {
105
+ const [s, i, c] = r.split(",").map(Number);
106
+ a.push({ r: s, g: i, b: c, count: e });
107
+ }), a;
108
+ }
109
+ function z(t) {
110
+ t.sort((e, r) => r.count - e.count);
111
+ const n = t.filter((e) => {
112
+ const { r, g: s, b: i } = e, c = Math.max(r, s, i), p = Math.min(r, s, i), d = c - p, f = c === 0 ? 0 : d / c, u = c / 255;
113
+ return f > 0.15 && u > 0.2 && u < 0.8;
114
+ }), o = n.length > 0 ? n[0] : t[0];
115
+ return m({ r: o.r, g: o.g, b: o.b }).hex();
116
+ }
117
+ function V(t) {
118
+ return new Promise((n, o) => {
119
+ if (!t.type.startsWith("image/")) {
120
+ o(new Error("请选择图片文件"));
121
+ return;
122
+ }
123
+ const a = new FileReader();
124
+ a.onload = async (e) => {
125
+ try {
126
+ const r = new Image();
127
+ r.onload = async () => {
128
+ try {
129
+ const s = await I(r);
130
+ n(s);
131
+ } catch (s) {
132
+ o(s);
133
+ }
134
+ }, r.onerror = () => o(new Error("图片加载失败")), r.src = e.target.result;
135
+ } catch (r) {
136
+ o(r);
137
+ }
138
+ }, a.onerror = () => o(new Error("文件读取失败")), a.readAsDataURL(t);
139
+ });
140
+ }
141
+ const M = {
142
+ red: "#F53F3F",
143
+ orangered: "#F77234",
144
+ orange: "#FF7D00",
145
+ gold: "#F7BA1E",
146
+ yellow: "#FADC19",
147
+ lime: "#9FDB1D",
148
+ green: "#00B42A",
149
+ cyan: "#14C9C9",
150
+ blue: "#3491FA",
151
+ arcoblue: "#165DFF",
152
+ purple: "#722ED1",
153
+ pinkpurple: "#D91AD9",
154
+ magenta: "#F5319D"
155
+ };
156
+ function $() {
157
+ const t = {};
158
+ return Object.keys(M).forEach((n) => {
159
+ t[n] = {}, t[n].light = D(M[n], { list: !0 }), t[n].dark = D(M[n], { list: !0, dark: !0 }), t[n].primary = M[n];
160
+ }), t.gray = {}, t.gray.light = [
161
+ "#f7f8fa",
162
+ "#f2f3f5",
163
+ "#e5e6eb",
164
+ "#c9cdd4",
165
+ "#a9aeb8",
166
+ "#86909c",
167
+ "#6b7785",
168
+ "#4e5969",
169
+ "#272e3b",
170
+ "#1d2129"
171
+ ], t.gray.dark = [
172
+ "#17171a",
173
+ "#2e2e30",
174
+ "#484849",
175
+ "#5f5f60",
176
+ "#78787a",
177
+ "#929293",
178
+ "#ababac",
179
+ "#c5c5c5",
180
+ "#dfdfdf",
181
+ "#f6f6f6"
182
+ ], t.gray.primary = t.gray.light[6], t;
183
+ }
184
+ export {
185
+ M as colorList,
186
+ V as extractColorFromFile,
187
+ I as extractColorFromImage,
188
+ D as generate,
189
+ $ as getPresetColors,
190
+ R as getRgbStr
191
+ };
@@ -0,0 +1,2 @@
1
+ export default colorPaletteDark;
2
+ declare function colorPaletteDark(originColor: any, i: any, format: any): any;
@@ -0,0 +1,2 @@
1
+ export default colorPalette;
2
+ declare function colorPalette(originColor: any, i: any, format: any): any;
@@ -0,0 +1,2 @@
1
+ export function getRgbStr(color: any): any;
2
+ export function getColorString(color: any, format: any): any;
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@aviala-design/color",
3
+ "version": "0.1.0",
4
+ "description": "Aviala Design color utils",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "test": "jest",
17
+ "build": "vite build",
18
+ "preview": "vite preview",
19
+ "prepublishOnly": "npm run build"
20
+ },
21
+ "keywords": [
22
+ "Color",
23
+ "utils"
24
+ ],
25
+ "author": "Aviala Design Team",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/aviala-design/color"
30
+ },
31
+ "dependencies": {
32
+ "color": "^3.1.3"
33
+ },
34
+ "devDependencies": {
35
+ "@types/color": "^3.0.6",
36
+ "@types/node": "^20.11.24",
37
+ "jest": "^26.6.3",
38
+ "typescript": "^5.3.3",
39
+ "vite": "^6.2.3",
40
+ "vite-plugin-dts": "^3.9.1"
41
+ },
42
+ "files": [
43
+ "dist",
44
+ "README.md",
45
+ "LICENSE"
46
+ ]
47
+ }