UNPKG

2.33 kBJavaScriptView Raw
1/*
2Copyright 2019 Adobe. All rights reserved.
3This file is licensed to you under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License. You may obtain a copy
5of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7Unless required by applicable law or agreed to in writing, software distributed under
8the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9OF ANY KIND, either express or implied. See the License for the specific language
10governing permissions and limitations under the License.
11*/
12
13const {
14 cArray,
15 convertColorValue,
16 createScale,
17 removeDuplicates,
18} = require('./utils');
19
20const { Color } = require('./color');
21
22class BackgroundColor extends Color {
23 get backgroundColorScale() {
24 if (!this._backgroundColorScale) {
25 this._generateColorScale();
26 }
27 return this._backgroundColorScale;
28 }
29
30 _generateColorScale() {
31 // This would create a 100 color value array based on all parameters,
32 // which can be used for sliding lightness as a background color
33
34 // Call original generateColorScale method in the context of our background color
35 // Then we can run the code for Color, but we've added in more below.
36 Color.prototype._generateColorScale.call(this);
37
38 // create massive scale
39 const backgroundColorScale = createScale({ swatches: 1000, colorKeys: this._colorKeys, colorspace: this._colorspace, shift: 1, smooth: this._smooth });
40
41 // Inject original keycolors to ensure they are present in the background options
42 backgroundColorScale.push(...this.colorKeys);
43
44 const colorObj = backgroundColorScale
45 // Convert to HSLuv and keep track of original indices
46 .map((c, i) => ({ value: Math.round(cArray(c)[2]), index: i }));
47
48 const bgColorArrayFiltered = removeDuplicates(colorObj, 'value')
49 .map((data) => backgroundColorScale[data.index]);
50
51 // Manually cap the background array at 100 colors, then add white back to the end
52 // since it sometimes gets removed.
53 bgColorArrayFiltered.length = 100;
54 bgColorArrayFiltered.push('#ffffff');
55
56 this._backgroundColorScale = bgColorArrayFiltered.map((color) => convertColorValue(color, this._output));
57
58 return this._backgroundColorScale;
59 }
60}
61module.exports = { BackgroundColor };