-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
214 lines (195 loc) · 4.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Types
/**
* @typedef {Object} RGBAColorUc
* @property {number} R
* @property {number} G
* @property {number} B
* @property {number} [A]
* @typedef {Object} RGBAColorLc
* @property {number} r
* @property {number} g
* @property {number} b
* @property {number} [a]
* @typedef {(RGBAColorUc | RGBAColorLc)} RGBAColor
* @typedef { {L: number, a: number, b: number} } LabColor
* @typedef {(RGBAColor | LabColor)} Color
* @typedef {Object<string,RGBAColor>} PaletteMap
* @typedef {Object<string,LabColor>} PaletteMapLab
*/
// Imports
import { rgbaToLab } from "./lib/convert.js";
import { ciede2000 } from "./lib/diff.js";
import {
mapPalette,
paletteMapKey,
matchPaletteLab,
mapPaletteLab,
labPaletteMapKey,
} from "./lib/palette.js";
// Exports
export {
closest,
closestLab,
furthest,
furthestLab,
mapPalette,
paletteMapKey,
matchPaletteLab,
mapPaletteLab,
labPaletteMapKey,
ciede2000 as diff,
rgbaToLab,
// eslint-disable-next-line camelcase
rgb_to_lab,
// eslint-disable-next-line camelcase
rgba_to_lab,
// eslint-disable-next-line camelcase
map_palette,
// eslint-disable-next-line camelcase
palette_map_key,
// eslint-disable-next-line camelcase
match_palette_lab,
// eslint-disable-next-line camelcase
map_palette_lab,
// eslint-disable-next-line camelcase
lab_palette_map_key,
// eslint-disable-next-line camelcase
closest_lab,
// eslint-disable-next-line camelcase
furthest_lab,
};
/**
* Returns the color in the palette closest to target, given the background color bc
* @param {RGBAColor} target
* @param {RGBAColor[]} relative
* @param {RGBAColor} bc
* @returns {RGBAColor}
*/
function closest(target, relative, bc) {
const key = paletteMapKey(target);
bc = bc || { R: 255, G: 255, B: 255 };
const result = mapPalette([ target ], relative, "closest", bc);
return result[key];
}
/**
* Returns the lab color in the palette closest to target
* @param {LabColor} target
* @param {LabColor[]} relative
* @returns {LabColor}
*/
function closestLab(target, relative) {
return matchPaletteLab(target, relative, false);
}
/**
* Returns the color in the palette furthest from target, given the background color bc
* @param {RGBAColor} target
* @param {RGBAColor[]} relative
* @param {RGBAColor} bc
* @returns {RGBAColor}
*/
function furthest(target, relative, bc) {
const key = paletteMapKey(target);
bc = bc || { R: 255, G: 255, B: 255 };
const result = mapPalette([ target ], relative, "furthest", bc);
return result[key];
}
/**
* Returns the color in the palette furthest from target, given the background color bc
* @param {LabColor} target
* @param {LabColor[]} relative
* @returns {LabColor}
*/
function furthestLab(target, relative) {
return matchPaletteLab(target, relative, true);
}
/** Deprecated function names in snake_case to remain backwards compatible */
/**
* @deprecated since version 1.3
* @param {RGBAColor[]} a
* @param {RGBAColor[]} b
* @param {('closest'|'furthest')} [type]
* @param {RGBAColor} [bc]
* @return {PaletteMap}
*/
// eslint-disable-next-line camelcase
function map_palette(a, b, type, bc) {
return mapPalette(a, b, type, bc);
}
/**
* @deprecated since version 1.3
* @param {RGBAColor} c
* @return {String}
*/
// eslint-disable-next-line camelcase
function palette_map_key(c) {
return paletteMapKey(c);
}
/**
* @deprecated since version 1.3
* @param {RGBAColor} c
* @return {LabColor} c
*/
// eslint-disable-next-line camelcase
function rgb_to_lab(c) {
return rgbaToLab(c);
}
/**
* @deprecated since version 1.3
* @param {RGBAColor} c
* @return {LabColor} c
*/
// eslint-disable-next-line camelcase
function rgba_to_lab(c) {
return rgbaToLab(c);
}
/**
* @deprecated since version 1.3
* @param {LabColor} targetColor
* @param {LabColor[]} palette
* @param {boolean} [findFurthest]
* @return {LabColor}
*/
// eslint-disable-next-line camelcase
function match_palette_lab(targetColor, palette, findFurthest) {
return matchPaletteLab(targetColor, palette, findFurthest);
}
/**
* @deprecated since version 1.3
* @param {LabColor[]} a
* @param {LabColor[]} b
* @param {('closest'|'furthest')} [type]
* @return {PaletteMapLab}
*/
// eslint-disable-next-line camelcase
function map_palette_lab(a, b, type) {
return mapPaletteLab(a, b, type);
}
/**
* @deprecated since version 1.3
* @param {LabColor} c
* @return {string}
*/
// eslint-disable-next-line camelcase
function lab_palette_map_key(c) {
return labPaletteMapKey(c);
}
/**
* @deprecated since version 1.3
* @param {LabColor} target
* @param {LabColor[]} relative
* @returns {LabColor}
*/
// eslint-disable-next-line camelcase
function closest_lab(target, relative) {
return closestLab(target, relative);
}
/**
* @deprecated since version 1.3
* @param {LabColor} target
* @param {LabColor[]} relative
* @returns {LabColor}
*/
// eslint-disable-next-line camelcase
function furthest_lab(target, relative) {
return furthestLab(target, relative);
}