| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 |
4x
4x
4x
4x
4x
4x
4x
148x
148x
148x
148x
148x
740x
148x
148x
148x
148x
148x
148x
148x
148x
134x
134x
148x
148x
148x
148x
148x
148x
148x
| // tslint:disable:member-ordering
// tslint:disable:prefer-for-of
import { blendMultiply, blendOverlay, saturateViaLCH } from "./colorBlending";
import { rgbToHSL } from "./colorConverters";
import { ColorHSL } from "./colorHSL";
import { ColorInterpolationSpace, interpolateByColorSpace } from "./colorInterpolation";
import { ColorRGBA64 } from "./colorRGBA64";
import { ColorScale } from "./colorScale";
export interface ColorPaletteConfig {
baseColor?: ColorRGBA64;
steps?: number;
interpolationMode?: ColorInterpolationSpace;
scaleColorLight?: ColorRGBA64;
scaleColorDark?: ColorRGBA64;
clipLight?: number;
clipDark?: number;
saturationAdjustmentCutoff?: number;
saturationLight?: number;
saturationDark?: number;
overlayLight?: number;
overlayDark?: number;
multiplyLight?: number;
multiplyDark?: number;
}
export class ColorPalette {
public static readonly defaultPaletteConfig: ColorPaletteConfig = {
baseColor: new ColorRGBA64(0.5, 0.5, 0.5, 1),
steps: 7,
interpolationMode: ColorInterpolationSpace.RGB,
scaleColorLight: new ColorRGBA64(1, 1, 1, 1),
scaleColorDark: new ColorRGBA64(0, 0, 0, 1),
clipLight: 0.185,
clipDark: 0.16,
saturationAdjustmentCutoff: 0.05,
saturationLight: 0.35,
saturationDark: 1.25,
overlayLight: 0,
overlayDark: 0.25,
multiplyLight: 0,
multiplyDark: 0,
};
constructor(config: ColorPaletteConfig) {
this.config = Object.assign({}, ColorPalette.defaultPaletteConfig, config);
this.palette = [];
this.updatePaletteColors();
}
private readonly config: ColorPaletteConfig;
public readonly palette: ColorRGBA64[];
public updatePaletteGenerationValues(newConfig: ColorPaletteConfig): boolean {
let changed: boolean = false;
for (const key in newConfig) {
if (this.config[key]) {
if (this.config[key].equalValue) {
if (!this.config[key].equalValue(newConfig[key])) {
this.config[key] = newConfig[key];
changed = true;
}
} else {
if (newConfig[key] !== this.config[key]) {
this.config[key] = newConfig[key];
changed = true;
}
}
}
}
if (changed) {
this.updatePaletteColors();
}
return changed;
}
private updatePaletteColors(): void {
const scale: ColorScale = this.generatePaletteColorScale();
for (let i: number = 0; i < this.config.steps; i++) {
this.palette[i] = scale.getColor(
i / (this.config.steps - 1),
this.config.interpolationMode
);
}
}
public generatePaletteColorScale(): ColorScale {
const baseColorHSL: ColorHSL = rgbToHSL(this.config.baseColor);
const baseScale: ColorScale = new ColorScale([
{ position: 0, color: this.config.scaleColorLight },
{ position: 0.5, color: this.config.baseColor },
{ position: 1, color: this.config.scaleColorDark },
]);
const trimmedScale: ColorScale = baseScale.trim(
this.config.clipLight,
1 - this.config.clipDark
);
const trimmedLight: ColorRGBA64 = trimmedScale.getColor(0);
const trimmedDark: ColorRGBA64 = trimmedScale.getColor(1);
let adjustedLight: ColorRGBA64 = trimmedLight;
let adjustedDark: ColorRGBA64 = trimmedDark;
if (baseColorHSL.s >= this.config.saturationAdjustmentCutoff) {
adjustedLight = saturateViaLCH(adjustedLight, this.config.saturationLight);
adjustedDark = saturateViaLCH(adjustedDark, this.config.saturationDark);
}
Iif (this.config.multiplyLight !== 0) {
const multiply: ColorRGBA64 = blendMultiply(
this.config.baseColor,
adjustedLight
);
adjustedLight = interpolateByColorSpace(
this.config.multiplyLight,
this.config.interpolationMode,
adjustedLight,
multiply
);
}
Iif (this.config.multiplyDark !== 0) {
const multiply: ColorRGBA64 = blendMultiply(
this.config.baseColor,
adjustedDark
);
adjustedDark = interpolateByColorSpace(
this.config.multiplyDark,
this.config.interpolationMode,
adjustedDark,
multiply
);
}
Iif (this.config.overlayLight !== 0) {
const overlay: ColorRGBA64 = blendOverlay(
this.config.baseColor,
adjustedLight
);
adjustedLight = interpolateByColorSpace(
this.config.overlayLight,
this.config.interpolationMode,
adjustedLight,
overlay
);
}
Eif (this.config.overlayDark !== 0) {
const overlay: ColorRGBA64 = blendOverlay(
this.config.baseColor,
adjustedDark
);
adjustedDark = interpolateByColorSpace(
this.config.overlayDark,
this.config.interpolationMode,
adjustedDark,
overlay
);
}
return new ColorScale([
{ position: 0, color: adjustedLight.clamp() },
{ position: 0.5, color: this.config.baseColor },
{ position: 1, color: adjustedDark.clamp() },
]);
}
}
|