|
| 1 | +/** |
| 2 | + * @author jessuni |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | +const _CONFIG = { |
| 7 | + color: [0, 0, 0], |
| 8 | + contrast: 4.5, |
| 9 | +} |
| 10 | + |
| 11 | +class SafeColor { |
| 12 | + /** |
| 13 | + * Create a SafeColor instance with options |
| 14 | + * @param {Array} color 8bit RGB value of a given color in the form of [R, G, B] |
| 15 | + * @param {Number} contrast contrast ratio of foreground color and background color https://www.w3.org/TR/WCAG20/#contrast-ratiodef |
| 16 | + */ |
| 17 | + constructor(options = {}) { |
| 18 | + this.color = options.color || _CONFIG.color |
| 19 | + this.contrast = options.contrast || _CONFIG.contrast |
| 20 | + this.hash = 0 |
| 21 | + } |
| 22 | + |
| 23 | + _rgb2hsl([r, g, b]) { |
| 24 | + r /= 255 |
| 25 | + g /= 255 |
| 26 | + b /= 255 |
| 27 | + const cmax = Math.max(r, g, b) |
| 28 | + const cmin = Math.min(r, g, b) |
| 29 | + const chroma = cmax - cmin |
| 30 | + let l = (cmax + cmin) / 2 |
| 31 | + let h, s |
| 32 | + if (!chroma) { |
| 33 | + h = s = 0 |
| 34 | + } else { |
| 35 | + s = chroma / (1 - Math.abs(2 * l - 1)) |
| 36 | + if (cmax === r) { |
| 37 | + h = ((g - b) / chroma) % 6 |
| 38 | + } else if (cmax === g) { |
| 39 | + h = (b - r) / chroma + 2 |
| 40 | + } else { |
| 41 | + h = (r - g) / chroma + 4 |
| 42 | + } |
| 43 | + h = Math.round(h * 60) |
| 44 | + h = h < 0 ? h + 360 : h |
| 45 | + } |
| 46 | + return [h, s, l] |
| 47 | + } |
| 48 | + |
| 49 | + _hsl2rgb([h, s, l]) { |
| 50 | + let c = (1 - Math.abs(2 * l - 1)) * s |
| 51 | + let x = c * (1 - Math.abs((h / 60) % 2 - 1)) |
| 52 | + let m = l - c / 2 |
| 53 | + let r = 0 |
| 54 | + let g = 0 |
| 55 | + let b = 0 |
| 56 | + if (h >= 0 && h < 60) { |
| 57 | + r = c |
| 58 | + g = x |
| 59 | + b = 0 |
| 60 | + } else if (h >= 60 && h < 120) { |
| 61 | + r = x |
| 62 | + g = c |
| 63 | + b = 0 |
| 64 | + } else if (h >= 120 && h < 180) { |
| 65 | + r = 0 |
| 66 | + g = c |
| 67 | + b = x |
| 68 | + } else if (h >= 180 && h < 240) { |
| 69 | + r = 0 |
| 70 | + g = x |
| 71 | + b = c |
| 72 | + } else if (h >= 240 && h < 300) { |
| 73 | + r = x |
| 74 | + g = 0 |
| 75 | + b = c |
| 76 | + } else if (h >= 300 && h < 360) { |
| 77 | + r = c |
| 78 | + g = 0 |
| 79 | + b = x |
| 80 | + } |
| 81 | + r = Math.round((r + m) * 255) |
| 82 | + g = Math.round((g + m) * 255) |
| 83 | + b = Math.round((b + m) * 255) |
| 84 | + return [r, g, b] |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Calculate the luma of a given sRGB array |
| 89 | + * @param {Array} sRGB |
| 90 | + * @return luma of the color |
| 91 | + */ |
| 92 | + calLuma(sRGB) { |
| 93 | + const [linearR, linearG, linearB] = sRGB.map(v => { |
| 94 | + const decimal = v / 255 |
| 95 | + return decimal <= 0.04045 ? decimal / 12.92 : Math.pow((decimal + 0.055) / 1.055, 2.4) |
| 96 | + }) |
| 97 | + return linearR * 0.2126 + linearG * 0.7152 + linearB * 0.0722 |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Calculate the valid luma range with the provided color and the contrast ratio |
| 102 | + * @param {Array} fgColor |
| 103 | + * @param {Number} contrastRatio |
| 104 | + */ |
| 105 | + calValidLumaRange(fgColor, contrastRatio) { |
| 106 | + const fgLuma = this.calLuma(fgColor) |
| 107 | + const edgeLuma = (fgLuma + 0.05) / contrastRatio - 0.05 |
| 108 | + if (edgeLuma < 1 && edgeLuma > 0) { |
| 109 | + this.minLuma = 0 |
| 110 | + this.maxLuma = edgeLuma |
| 111 | + } else if (edgeLuma === 1 || edgeLuma === 0) { |
| 112 | + this.minLuma = this.maxLuma = edgeLuma |
| 113 | + } else { |
| 114 | + this.minLuma = (fgLuma + 0.05) * contrastRatio |
| 115 | + this.maxLuma = 1 |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Check if generated color's luma is within the valid luma range. If not, launder the color until it is |
| 121 | + * @param {Array} color |
| 122 | + */ |
| 123 | + launderColor(color) { |
| 124 | + const luma = this.calLuma(color) |
| 125 | + if (luma >= this.minLuma && luma <= this.maxLuma) { |
| 126 | + return color |
| 127 | + } else { |
| 128 | + const hsl = this._rgb2hsl(color) |
| 129 | + if (luma < this.minLuma) { |
| 130 | + const step = this.minLuma - luma |
| 131 | + hsl[2] = this.hash ? step + luma : (1 - hsl[2]) * Math.random() + hsl[2] |
| 132 | + color = this._hsl2rgb(hsl) |
| 133 | + return this.launderColor(this.color, color) |
| 134 | + } else { |
| 135 | + const step = luma - this.maxLuma |
| 136 | + hsl[2] = this.hash ? luma - step : Math.random() % hsl[2] |
| 137 | + color = this._hsl2rgb(hsl) |
| 138 | + return this.launderColor(this.color, color) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + /** |
| 143 | + * Generates a random color that meets (>=) the contrast ratio. If a string is passed in, generate a consistent contrast-safe color for that string. |
| 144 | + * @param {String} str |
| 145 | + */ |
| 146 | + random(str) { |
| 147 | + this.calValidLumaRange(this.color, this.contrast) |
| 148 | + if (this.minLuma === this.maxLuma) { |
| 149 | + this.genColor = this.minLuma ? [255, 255, 255] : [0, 0, 0] |
| 150 | + return |
| 151 | + } |
| 152 | + if (!str) { |
| 153 | + this.genColor = [Math.random() * 255, Math.random() * 255, Math.random() * 255] |
| 154 | + } else { |
| 155 | + for (let i = 0; i < str.length; i++) { |
| 156 | + this.hash = str.charCodeAt(i) + ((this.hash << 5) - this.hash) |
| 157 | + this.hash = this.hash & this.hash |
| 158 | + } |
| 159 | + this.genColor = [0, 0, 0] |
| 160 | + this.genColor = this.genColor.map((v, index) => (v = (this.hash >> (index * 8)) & 255)) |
| 161 | + } |
| 162 | + this.genColor = this.launderColor(this.color, this.genColor) |
| 163 | + return 'rgb(' + this.genColor + ')' |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +export default SafeColor |
0 commit comments