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

Method _createColorScale

docs/ui/src/js/initialSequentialScale.js:151–224  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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,

Callers 8

constructorMethod · 0.95
colorKeysMethod · 0.95
colorSpaceMethod · 0.95
smoothMethod · 0.95
distributeLightnessMethod · 0.95
outputMethod · 0.95
shiftMethod · 0.95
swatchesMethod · 0.95

Calls 5

roundFunction · 0.90
findMatchingLuminosityFunction · 0.90
convertColorValueFunction · 0.90
orderColorsByLuminosityFunction · 0.90
fillRangeFunction · 0.70

Tested by

no test coverage detected