()
| 149 | } |
| 150 | |
| 151 | _createColorScale() { |
| 152 | if (this._colors) this._colors = null; |
| 153 | if (this._colorsReversed) this._colorsReversed = null; |
| 154 | |
| 155 | let colorScale; |
| 156 | let generousColorLength = this._distributeLightness === 'parabolic' || this._distributeLightness === 'polynomial' ? 100 : 12; |
| 157 | let initialColorScale = Leo.createScale({ |
| 158 | swatches: generousColorLength, |
| 159 | colorKeys: this._colorKeys, |
| 160 | colorSpace: this._colorspace, |
| 161 | shift: this._shift, |
| 162 | distributeLightness: this._distributeLightness, |
| 163 | smooth: this._smooth, |
| 164 | fullScale: false, |
| 165 | asFun: true |
| 166 | }); |
| 167 | |
| 168 | const minLum = Math.min(...this._luminosities); |
| 169 | const maxLum = Math.max(...this._luminosities); |
| 170 | const maxLumShifted = maxLum - minLum; |
| 171 | |
| 172 | const fillRange = (start, end) => { |
| 173 | return Array(end - start) |
| 174 | .fill() |
| 175 | .map((item, index) => start + index); |
| 176 | }; |
| 177 | let dataX = fillRange(0, generousColorLength); |
| 178 | dataX = dataX.map((x) => (x === 0 ? 0 : x / (generousColorLength - 1))); |
| 179 | |
| 180 | let newLums = dataX.map((i) => round(maxLumShifted * i + minLum, 2)); |
| 181 | |
| 182 | let newColors = findMatchingLuminosity(initialColorScale, generousColorLength, newLums, this._smooth); |
| 183 | |
| 184 | newColors = [...new Set(newColors)]; |
| 185 | const lastColorIndex = newColors.length - 1; |
| 186 | // Manually ensure first and last user-input key colors |
| 187 | // are part of new key colors array being passed to the |
| 188 | // new color scale. |
| 189 | const first = this._smooth ? chroma(initialColorScale(0)) : initialColorScale(0); |
| 190 | const last = this._smooth ? chroma(initialColorScale(generousColorLength)) : initialColorScale(generousColorLength); |
| 191 | newColors.splice(0, 1, first.hex()); |
| 192 | newColors.splice(lastColorIndex, 1); |
| 193 | newColors.splice(lastColorIndex, 1, last.hex()); |
| 194 | |
| 195 | this._colorFunction = Leo.createScale({ |
| 196 | swatches: this._swatches, |
| 197 | colorKeys: newColors, |
| 198 | colorSpace: this._colorspace, |
| 199 | shift: this._shift, |
| 200 | smooth: false, |
| 201 | distributeLightness: this._distributeLightness, |
| 202 | fullScale: false, |
| 203 | asFun: true |
| 204 | }); |
| 205 | |
| 206 | colorScale = Leo.createScale({ |
| 207 | swatches: this._swatches, |
| 208 | colorKeys: newColors, |
no test coverage detected