(value, colorSpaces, background)
| 92 | } |
| 93 | |
| 94 | function createColorJson(value, colorSpaces, background) { |
| 95 | if (!chroma.valid(value)) { |
| 96 | // Should return an error |
| 97 | } |
| 98 | // If it's a valid color input, do this stuff... |
| 99 | else { |
| 100 | let color = chroma(value); |
| 101 | |
| 102 | let colorObj = {}; |
| 103 | |
| 104 | for (let i = 0; i < colorSpaces.length; i++) { |
| 105 | let colorChannels, colorChannelLabels; |
| 106 | if (colorSpaces[i] === 'HEX') colorChannels = color.hex(); |
| 107 | if (colorSpaces[i] === 'RGB') colorChannels = color.rgb(); |
| 108 | if (colorSpaces[i] === 'HSL') |
| 109 | colorChannels = color.hsl().map((c, i) => { |
| 110 | if (i > 0) return c * 100; |
| 111 | else return c; |
| 112 | }); // all channels except hue are percentages |
| 113 | if (colorSpaces[i] === 'HSV') |
| 114 | colorChannels = color.hsv().map((c, i) => { |
| 115 | if (i > 0) return c * 100; |
| 116 | else return c; |
| 117 | }); // all channels except hue are percentages |
| 118 | if (colorSpaces[i] === 'LAB') colorChannels = color.lab(); |
| 119 | if (colorSpaces[i] === 'LCH') colorChannels = color.lch(); |
| 120 | if (colorSpaces[i] === 'OKLAB') colorChannels = color.oklab(); |
| 121 | if (colorSpaces[i] === 'OKLCH') colorChannels = color.oklch(); |
| 122 | if (colorSpaces[i] === 'HSLuv') colorChannels = color.hsluv(); |
| 123 | if (colorSpaces[i] === 'CAM02') colorChannels = color.jab(); |
| 124 | if (colorSpaces[i] === 'CAM02p') colorChannels = color.jch(); |
| 125 | if (colorSpaces[i] === 'CMYK') |
| 126 | colorChannels = color.cmyk().map((c) => { |
| 127 | return c * 100; |
| 128 | }); |
| 129 | if (colorSpaces[i] === 'XYZ') |
| 130 | colorChannels = Object.values( |
| 131 | new simpleColorConverter({ |
| 132 | rgb: {r: color.rgb()[0], g: color.rgb()[1], b: color.rgb()[2]}, |
| 133 | to: 'xyz' |
| 134 | }).color |
| 135 | ); |
| 136 | if (colorSpaces[i] === 'Pantone') |
| 137 | colorChannels = new simpleColorConverter({ |
| 138 | rgb: {r: color.rgb()[0], g: color.rgb()[1], b: color.rgb()[2]}, |
| 139 | to: 'pantone' |
| 140 | }).color; |
| 141 | if (colorSpaces[i] === 'wcag2') colorChannels = round(contrast(color.rgb(), chroma(background).rgb(), 'wcag2'), 2); |
| 142 | if (colorSpaces[i] === 'wcag3') { |
| 143 | const baseLightness = chroma(background).hsluv()[2]; |
| 144 | const baseV = round(baseLightness / 100, 2); |
| 145 | let base = chroma(background).rgb(); |
| 146 | colorChannels = round(baseV < 0.5 ? APCAcontrast(sRGBtoY(color.rgb()), sRGBtoY(base)) * -1 : APCAcontrast(sRGBtoY(color.rgb()), sRGBtoY(base)), 2); |
| 147 | } |
| 148 | |
| 149 | if (colorSpaces[i] === 'HEX') colorChannelLabels = ['Hex']; |
| 150 | if (colorSpaces[i] === 'RGB') colorChannelLabels = ['rgb (R)', 'rgb (G)', 'rgb (B)']; |
| 151 | if (colorSpaces[i] === 'HSL') colorChannelLabels = ['hsl (H)', 'hsl (S)', 'hsl (L)']; |
no test coverage detected