MCPcopy Create free account
hub / github.com/adobe/leonardo / convertColorValue

Function convertColorValue

docs/ui/src/js/utils.js:182–247  ·  view source on GitHub ↗
(color, format, object = false)

Source from the content-addressed store, hash-verified

180
181window.convertColorValue = convertColorValue;
182function convertColorValue(color, format, object = false) {
183 if (!color) {
184 throw new Error(`Cannot convert color value of “${color}”`);
185 }
186 if (!colorSpaces[format]) {
187 throw new Error(`Cannot convert to colorspace “${format}”`);
188 }
189
190 const space = colorSpaces[format];
191 const colorObj = chroma(String(color))[space]();
192 if (format === 'HSL') {
193 colorObj.pop();
194 }
195 if (format === 'HEX') {
196 if (object) {
197 const rgb = chroma(String(color)).rgb();
198 return {r: rgb[0], g: rgb[1], b: rgb[2]};
199 }
200 return colorObj;
201 }
202
203 const colorObject = {};
204 let newColorObj = colorObj.map(filterNaN);
205
206 newColorObj = newColorObj.map((ch, i) => {
207 let rnd = round(ch);
208 let j = i;
209 if (space === 'hsluv') {
210 j += 2;
211 }
212 let letter = space.charAt(j);
213 if (space === 'jch' && letter === 'c') {
214 letter = 'C';
215 }
216 colorObject[letter === 'j' ? 'J' : letter] = rnd;
217 if (space in {lab: 1, lch: 1, jab: 1, jch: 1}) {
218 if (!object) {
219 if (letter === 'l' || letter === 'j') {
220 rnd += '%';
221 }
222 if (letter === 'h') {
223 rnd += 'deg';
224 }
225 }
226 } else if (space !== 'hsluv') {
227 if (letter === 's' || letter === 'l' || letter === 'v') {
228 colorObject[letter] = round(ch, 2);
229 if (!object) {
230 rnd = round(ch * 100);
231 rnd += '%';
232 }
233 } else if (letter === 'h' && !object) {
234 rnd += 'deg';
235 }
236 }
237 return rnd;
238 });
239

Callers 5

outputMethod · 0.90
_createColorScaleMethod · 0.90
outputMethod · 0.90
_createColorScaleMethod · 0.90
bulkConvertColorValueFunction · 0.70

Calls 1

roundFunction · 0.70

Tested by

no test coverage detected