material 3.7.3 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +223 -0
- package/README.md +5 -3
- package/dist/components/extended-fab/types.d.ts +3 -0
- package/dist/components/fab/types.d.ts +3 -0
- package/dist/elements/extended-fab.d.ts +1 -0
- package/dist/elements/fab.d.ts +1 -0
- package/dist/elements/preupgrade/badge.css +1 -1
- package/dist/elements/preupgrade/bottom-app-bar.css +1 -1
- package/dist/elements/preupgrade/bottom-sheet.css +1 -1
- package/dist/elements/preupgrade/button-group.css +1 -1
- package/dist/elements/preupgrade/button.css +1 -1
- package/dist/elements/preupgrade/card.css +1 -1
- package/dist/elements/preupgrade/carousel.css +1 -1
- package/dist/elements/preupgrade/checkbox.css +1 -1
- package/dist/elements/preupgrade/chips.css +1 -1
- package/dist/elements/preupgrade/datepicker.css +1 -1
- package/dist/elements/preupgrade/dialog.css +1 -1
- package/dist/elements/preupgrade/divider.css +1 -1
- package/dist/elements/preupgrade/drawer.css +1 -1
- package/dist/elements/preupgrade/extended-fab.css +1 -1
- package/dist/elements/preupgrade/fab-menu.css +1 -1
- package/dist/elements/preupgrade/fab.css +1 -1
- package/dist/elements/preupgrade/icon-button.css +1 -1
- package/dist/elements/preupgrade/list.css +1 -1
- package/dist/elements/preupgrade/loading-indicator.css +1 -1
- package/dist/elements/preupgrade/menu.css +1 -1
- package/dist/elements/preupgrade/navigation-bar.css +1 -1
- package/dist/elements/preupgrade/navigation-rail.css +1 -1
- package/dist/elements/preupgrade/progress.css +1 -1
- package/dist/elements/preupgrade/radios.css +1 -1
- package/dist/elements/preupgrade/search.css +1 -1
- package/dist/elements/preupgrade/select.css +1 -1
- package/dist/elements/preupgrade/side-sheet.css +1 -1
- package/dist/elements/preupgrade/slider.css +1 -1
- package/dist/elements/preupgrade/snackbar.css +1 -1
- package/dist/elements/preupgrade/split-button.css +1 -1
- package/dist/elements/preupgrade/switch.css +1 -1
- package/dist/elements/preupgrade/tabs.css +1 -1
- package/dist/elements/preupgrade/text-field.css +1 -1
- package/dist/elements/preupgrade/timepicker.css +1 -1
- package/dist/elements/preupgrade/toolbar.css +1 -1
- package/dist/elements/preupgrade/tooltip.css +1 -1
- package/dist/elements/preupgrade/top-app-bar.css +1 -1
- package/dist/elements/preupgrade.css +1 -1
- package/dist/styles/badge.css +1 -1
- package/dist/styles/base-lite.css +2 -2
- package/dist/styles/base.css +2 -2
- package/dist/styles/bottom-app-bar.css +1 -1
- package/dist/styles/bottom-sheet.css +1 -1
- package/dist/styles/button-group.css +1 -1
- package/dist/styles/button.css +1 -1
- package/dist/styles/card.css +1 -1
- package/dist/styles/carousel.css +1 -1
- package/dist/styles/checkbox.css +1 -1
- package/dist/styles/chips.css +1 -1
- package/dist/styles/contrast.css +2 -2
- package/dist/styles/datepicker.css +1 -1
- package/dist/styles/dialog.css +1 -1
- package/dist/styles/divider.css +1 -1
- package/dist/styles/drawer.css +1 -1
- package/dist/styles/extended-fab.css +1 -1
- package/dist/styles/fab-menu.css +1 -1
- package/dist/styles/fab.css +1 -1
- package/dist/styles/icon-button.css +1 -1
- package/dist/styles/list.css +1 -1
- package/dist/styles/loading-indicator.css +1 -1
- package/dist/styles/menu.css +1 -1
- package/dist/styles/navigation-bar.css +1 -1
- package/dist/styles/navigation-rail.css +1 -1
- package/dist/styles/progress.css +1 -1
- package/dist/styles/radios.css +1 -1
- package/dist/styles/search.css +1 -1
- package/dist/styles/select.css +1 -1
- package/dist/styles/side-sheet.css +1 -1
- package/dist/styles/slider.css +1 -1
- package/dist/styles/snackbar.css +1 -1
- package/dist/styles/split-button.css +1 -1
- package/dist/styles/switch.css +1 -1
- package/dist/styles/tabs.css +1 -1
- package/dist/styles/text-field.css +1 -1
- package/dist/styles/timepicker.css +1 -1
- package/dist/styles/toolbar.css +1 -1
- package/dist/styles/tooltip.css +1 -1
- package/dist/styles/top-app-bar.css +1 -1
- package/dist/styles/typography.css +1 -1
- package/dist/styles/utilities.css +1 -1
- package/dist/styles.css +2 -2
- package/dist/theme/image.d.ts +2 -0
- package/dist/theme/image.js +5 -0
- package/dist/theme/index.d.ts +23 -0
- package/dist/theme/index.js +32 -0
- package/dist/theme/roles.js +68 -0
- package/dist/theme/variants.d.ts +9 -0
- package/dist/theme/variants.js +17 -0
- package/dist/theme/vendor/contrast/contrast.js +72 -0
- package/dist/theme/vendor/dislike/dislike_analyzer.js +31 -0
- package/dist/theme/vendor/dynamiccolor/color_spec_2021.d.ts +63 -0
- package/dist/theme/vendor/dynamiccolor/color_spec_2021.js +663 -0
- package/dist/theme/vendor/dynamiccolor/contrast_curve.js +42 -0
- package/dist/theme/vendor/dynamiccolor/dynamic_color.d.ts +24 -0
- package/dist/theme/vendor/dynamiccolor/dynamic_color.js +264 -0
- package/dist/theme/vendor/dynamiccolor/dynamic_scheme.d.ts +87 -0
- package/dist/theme/vendor/dynamiccolor/dynamic_scheme.js +418 -0
- package/dist/theme/vendor/dynamiccolor/material_dynamic_colors.d.ts +124 -0
- package/dist/theme/vendor/dynamiccolor/material_dynamic_colors.js +311 -0
- package/dist/theme/vendor/dynamiccolor/tone_delta_pair.js +27 -0
- package/dist/theme/vendor/dynamiccolor/variant.js +28 -0
- package/dist/theme/vendor/hct/cam16.js +249 -0
- package/dist/theme/vendor/hct/hct.d.ts +23 -0
- package/dist/theme/vendor/hct/hct.js +86 -0
- package/dist/theme/vendor/hct/hct_solver.js +390 -0
- package/dist/theme/vendor/hct/viewing_conditions.js +73 -0
- package/dist/theme/vendor/palettes/tonal_palette.js +114 -0
- package/dist/theme/vendor/quantize/lab_point_provider.js +31 -0
- package/dist/theme/vendor/quantize/quantizer_celebi.js +25 -0
- package/dist/theme/vendor/quantize/quantizer_map.js +31 -0
- package/dist/theme/vendor/quantize/quantizer_wsmeans.js +178 -0
- package/dist/theme/vendor/quantize/quantizer_wu.js +342 -0
- package/dist/theme/vendor/scheme/scheme_content.js +30 -0
- package/dist/theme/vendor/scheme/scheme_expressive.js +30 -0
- package/dist/theme/vendor/scheme/scheme_fidelity.js +30 -0
- package/dist/theme/vendor/scheme/scheme_monochrome.js +30 -0
- package/dist/theme/vendor/scheme/scheme_neutral.js +30 -0
- package/dist/theme/vendor/scheme/scheme_tonal_spot.js +30 -0
- package/dist/theme/vendor/scheme/scheme_vibrant.js +30 -0
- package/dist/theme/vendor/score/score.js +100 -0
- package/dist/theme/vendor/temperature/temperature_cache.js +198 -0
- package/dist/theme/vendor/utils/color_utils.js +173 -0
- package/dist/theme/vendor/utils/image_utils.js +72 -0
- package/dist/theme/vendor/utils/math_utils.js +75 -0
- package/dist/theme/vendor/utils/string_utils.js +61 -0
- package/dist/themes/autumn-contrast.css +2 -2
- package/dist/themes/autumn.css +2 -2
- package/dist/themes/baseline-contrast.css +2 -2
- package/dist/themes/baseline.css +2 -2
- package/dist/themes/brownbeige-contrast.css +2 -2
- package/dist/themes/brownbeige.css +2 -2
- package/dist/themes/content-contrast.css +2 -2
- package/dist/themes/content.css +2 -2
- package/dist/themes/desert-contrast.css +2 -2
- package/dist/themes/desert.css +2 -2
- package/dist/themes/expressive-contrast.css +2 -2
- package/dist/themes/expressive.css +2 -2
- package/dist/themes/fidelity-contrast.css +2 -2
- package/dist/themes/fidelity.css +2 -2
- package/dist/themes/forest-contrast.css +2 -2
- package/dist/themes/forest.css +2 -2
- package/dist/themes/fruit-salad-contrast.css +2 -2
- package/dist/themes/fruit-salad.css +2 -2
- package/dist/themes/highcontrast-contrast.css +2 -2
- package/dist/themes/highcontrast.css +2 -2
- package/dist/themes/monochrome-contrast.css +2 -2
- package/dist/themes/monochrome.css +2 -2
- package/dist/themes/neutral-contrast.css +2 -2
- package/dist/themes/neutral.css +2 -2
- package/dist/themes/ocean-contrast.css +2 -2
- package/dist/themes/ocean.css +2 -2
- package/dist/themes/rainbow-contrast.css +2 -2
- package/dist/themes/rainbow.css +2 -2
- package/dist/themes/sageivory-contrast.css +2 -2
- package/dist/themes/sageivory.css +2 -2
- package/dist/themes/spring-contrast.css +2 -2
- package/dist/themes/spring.css +2 -2
- package/dist/themes/summer-contrast.css +2 -2
- package/dist/themes/summer.css +2 -2
- package/dist/themes/sunset-contrast.css +2 -2
- package/dist/themes/sunset.css +2 -2
- package/dist/themes/tealcaramel-contrast.css +2 -2
- package/dist/themes/tealcaramel.css +2 -2
- package/dist/themes/vibrant-contrast.css +2 -2
- package/dist/themes/vibrant.css +2 -2
- package/package.json +14 -1
- package/src/styles/themes/_autumn.scss +20 -2
- package/src/styles/themes/_base-theme.scss +34 -24
- package/src/styles/themes/_baseline.scss +18 -0
- package/src/styles/themes/_brownbeige.scss +30 -2
- package/src/styles/themes/_content.scss +30 -2
- package/src/styles/themes/_desert.scss +30 -2
- package/src/styles/themes/_expressive.scss +30 -2
- package/src/styles/themes/_fidelity.scss +30 -2
- package/src/styles/themes/_forest.scss +20 -2
- package/src/styles/themes/_fruit-salad.scss +30 -2
- package/src/styles/themes/_highcontrast.scss +21 -2
- package/src/styles/themes/_monochrome.scss +30 -2
- package/src/styles/themes/_neutral.scss +30 -2
- package/src/styles/themes/_ocean.scss +20 -2
- package/src/styles/themes/_rainbow.scss +30 -2
- package/src/styles/themes/_sageivory.scss +30 -2
- package/src/styles/themes/_spring.scss +20 -2
- package/src/styles/themes/_summer.scss +30 -2
- package/src/styles/themes/_sunset.scss +20 -2
- package/src/styles/themes/_tealcaramel.scss +30 -2
- package/src/styles/themes/_vibrant.scss +30 -2
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { DynamicScheme } from "../dynamiccolor/dynamic_scheme.js";
|
|
18
|
+
import { Variant } from "../dynamiccolor/variant.js";
|
|
19
|
+
export class SchemeNeutral extends DynamicScheme {
|
|
20
|
+
constructor(sourceColorHct, isDark, contrastLevel, specVersion = DynamicScheme.DEFAULT_SPEC_VERSION, platform = DynamicScheme.DEFAULT_PLATFORM) {
|
|
21
|
+
super({
|
|
22
|
+
sourceColorHct,
|
|
23
|
+
variant: Variant.NEUTRAL,
|
|
24
|
+
contrastLevel,
|
|
25
|
+
isDark,
|
|
26
|
+
platform,
|
|
27
|
+
specVersion,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { DynamicScheme } from "../dynamiccolor/dynamic_scheme.js";
|
|
18
|
+
import { Variant } from "../dynamiccolor/variant.js";
|
|
19
|
+
export class SchemeTonalSpot extends DynamicScheme {
|
|
20
|
+
constructor(sourceColorHct, isDark, contrastLevel, specVersion = DynamicScheme.DEFAULT_SPEC_VERSION, platform = DynamicScheme.DEFAULT_PLATFORM) {
|
|
21
|
+
super({
|
|
22
|
+
sourceColorHct,
|
|
23
|
+
variant: Variant.TONAL_SPOT,
|
|
24
|
+
contrastLevel,
|
|
25
|
+
isDark,
|
|
26
|
+
platform,
|
|
27
|
+
specVersion,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { DynamicScheme } from "../dynamiccolor/dynamic_scheme.js";
|
|
18
|
+
import { Variant } from "../dynamiccolor/variant.js";
|
|
19
|
+
export class SchemeVibrant extends DynamicScheme {
|
|
20
|
+
constructor(sourceColorHct, isDark, contrastLevel, specVersion = DynamicScheme.DEFAULT_SPEC_VERSION, platform = DynamicScheme.DEFAULT_PLATFORM) {
|
|
21
|
+
super({
|
|
22
|
+
sourceColorHct,
|
|
23
|
+
variant: Variant.VIBRANT,
|
|
24
|
+
contrastLevel,
|
|
25
|
+
isDark,
|
|
26
|
+
platform,
|
|
27
|
+
specVersion,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { Hct } from "../hct/hct.js";
|
|
18
|
+
import * as math from "../utils/math_utils.js";
|
|
19
|
+
const SCORE_OPTION_DEFAULTS = {
|
|
20
|
+
desired: 4,
|
|
21
|
+
fallbackColorARGB: 0xff4285f4,
|
|
22
|
+
filter: true,
|
|
23
|
+
};
|
|
24
|
+
function compare(a, b) {
|
|
25
|
+
if (a.score > b.score) {
|
|
26
|
+
return -1;
|
|
27
|
+
}
|
|
28
|
+
else if (a.score < b.score) {
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
export class Score {
|
|
34
|
+
constructor() { }
|
|
35
|
+
static score(colorsToPopulation, options) {
|
|
36
|
+
const { desired, fallbackColorARGB, filter } = { ...SCORE_OPTION_DEFAULTS, ...options };
|
|
37
|
+
const colorsHct = [];
|
|
38
|
+
const huePopulation = new Array(360).fill(0);
|
|
39
|
+
let populationSum = 0;
|
|
40
|
+
for (const [argb, population] of colorsToPopulation.entries()) {
|
|
41
|
+
const hct = Hct.fromInt(argb);
|
|
42
|
+
colorsHct.push(hct);
|
|
43
|
+
const hue = Math.floor(hct.hue);
|
|
44
|
+
huePopulation[hue] += population;
|
|
45
|
+
populationSum += population;
|
|
46
|
+
}
|
|
47
|
+
const hueExcitedProportions = new Array(360).fill(0.0);
|
|
48
|
+
for (let hue = 0; hue < 360; hue++) {
|
|
49
|
+
const proportion = huePopulation[hue] / populationSum;
|
|
50
|
+
for (let i = hue - 14; i < hue + 16; i++) {
|
|
51
|
+
const neighborHue = math.sanitizeDegreesInt(i);
|
|
52
|
+
hueExcitedProportions[neighborHue] += proportion;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const scoredHct = new Array();
|
|
56
|
+
for (const hct of colorsHct) {
|
|
57
|
+
const hue = math.sanitizeDegreesInt(Math.round(hct.hue));
|
|
58
|
+
const proportion = hueExcitedProportions[hue];
|
|
59
|
+
if (filter && (hct.chroma < Score.CUTOFF_CHROMA || proportion <= Score.CUTOFF_EXCITED_PROPORTION)) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const proportionScore = proportion * 100.0 * Score.WEIGHT_PROPORTION;
|
|
63
|
+
const chromaWeight = hct.chroma < Score.TARGET_CHROMA ? Score.WEIGHT_CHROMA_BELOW : Score.WEIGHT_CHROMA_ABOVE;
|
|
64
|
+
const chromaScore = (hct.chroma - Score.TARGET_CHROMA) * chromaWeight;
|
|
65
|
+
const score = proportionScore + chromaScore;
|
|
66
|
+
scoredHct.push({ hct, score });
|
|
67
|
+
}
|
|
68
|
+
scoredHct.sort(compare);
|
|
69
|
+
const chosenColors = [];
|
|
70
|
+
for (let differenceDegrees = 90; differenceDegrees >= 15; differenceDegrees--) {
|
|
71
|
+
chosenColors.length = 0;
|
|
72
|
+
for (const { hct } of scoredHct) {
|
|
73
|
+
const duplicateHue = chosenColors.find(chosenHct => {
|
|
74
|
+
return math.differenceDegrees(hct.hue, chosenHct.hue) < differenceDegrees;
|
|
75
|
+
});
|
|
76
|
+
if (!duplicateHue) {
|
|
77
|
+
chosenColors.push(hct);
|
|
78
|
+
}
|
|
79
|
+
if (chosenColors.length >= desired)
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
if (chosenColors.length >= desired)
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
const colors = [];
|
|
86
|
+
if (chosenColors.length === 0) {
|
|
87
|
+
colors.push(fallbackColorARGB);
|
|
88
|
+
}
|
|
89
|
+
for (const chosenHct of chosenColors) {
|
|
90
|
+
colors.push(chosenHct.toInt());
|
|
91
|
+
}
|
|
92
|
+
return colors;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
Score.TARGET_CHROMA = 48.0;
|
|
96
|
+
Score.WEIGHT_PROPORTION = 0.7;
|
|
97
|
+
Score.WEIGHT_CHROMA_ABOVE = 0.3;
|
|
98
|
+
Score.WEIGHT_CHROMA_BELOW = 0.1;
|
|
99
|
+
Score.CUTOFF_CHROMA = 5.0;
|
|
100
|
+
Score.CUTOFF_EXCITED_PROPORTION = 0.01;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { Hct } from "../hct/hct.js";
|
|
18
|
+
import * as colorUtils from "../utils/color_utils.js";
|
|
19
|
+
import * as mathUtils from "../utils/math_utils.js";
|
|
20
|
+
export class TemperatureCache {
|
|
21
|
+
constructor(input) {
|
|
22
|
+
this.input = input;
|
|
23
|
+
this.hctsByTempCache = [];
|
|
24
|
+
this.hctsByHueCache = [];
|
|
25
|
+
this.tempsByHctCache = new Map();
|
|
26
|
+
this.inputRelativeTemperatureCache = -1.0;
|
|
27
|
+
this.complementCache = null;
|
|
28
|
+
}
|
|
29
|
+
get hctsByTemp() {
|
|
30
|
+
if (this.hctsByTempCache.length > 0) {
|
|
31
|
+
return this.hctsByTempCache;
|
|
32
|
+
}
|
|
33
|
+
const hcts = this.hctsByHue.concat([this.input]);
|
|
34
|
+
const temperaturesByHct = this.tempsByHct;
|
|
35
|
+
hcts.sort((a, b) => temperaturesByHct.get(a) - temperaturesByHct.get(b));
|
|
36
|
+
this.hctsByTempCache = hcts;
|
|
37
|
+
return hcts;
|
|
38
|
+
}
|
|
39
|
+
get warmest() {
|
|
40
|
+
return this.hctsByTemp[this.hctsByTemp.length - 1];
|
|
41
|
+
}
|
|
42
|
+
get coldest() {
|
|
43
|
+
return this.hctsByTemp[0];
|
|
44
|
+
}
|
|
45
|
+
analogous(count = 5, divisions = 12) {
|
|
46
|
+
const startHue = Math.round(this.input.hue);
|
|
47
|
+
const startHct = this.hctsByHue[startHue];
|
|
48
|
+
let lastTemp = this.relativeTemperature(startHct);
|
|
49
|
+
const allColors = [startHct];
|
|
50
|
+
let absoluteTotalTempDelta = 0.0;
|
|
51
|
+
for (let i = 0; i < 360; i++) {
|
|
52
|
+
const hue = mathUtils.sanitizeDegreesInt(startHue + i);
|
|
53
|
+
const hct = this.hctsByHue[hue];
|
|
54
|
+
const temp = this.relativeTemperature(hct);
|
|
55
|
+
const tempDelta = Math.abs(temp - lastTemp);
|
|
56
|
+
lastTemp = temp;
|
|
57
|
+
absoluteTotalTempDelta += tempDelta;
|
|
58
|
+
}
|
|
59
|
+
let hueAddend = 1;
|
|
60
|
+
const tempStep = absoluteTotalTempDelta / divisions;
|
|
61
|
+
let totalTempDelta = 0.0;
|
|
62
|
+
lastTemp = this.relativeTemperature(startHct);
|
|
63
|
+
while (allColors.length < divisions) {
|
|
64
|
+
const hue = mathUtils.sanitizeDegreesInt(startHue + hueAddend);
|
|
65
|
+
const hct = this.hctsByHue[hue];
|
|
66
|
+
const temp = this.relativeTemperature(hct);
|
|
67
|
+
const tempDelta = Math.abs(temp - lastTemp);
|
|
68
|
+
totalTempDelta += tempDelta;
|
|
69
|
+
const desiredTotalTempDeltaForIndex = allColors.length * tempStep;
|
|
70
|
+
let indexSatisfied = totalTempDelta >= desiredTotalTempDeltaForIndex;
|
|
71
|
+
let indexAddend = 1;
|
|
72
|
+
while (indexSatisfied && allColors.length < divisions) {
|
|
73
|
+
allColors.push(hct);
|
|
74
|
+
const desiredTotalTempDeltaForIndex = ((allColors.length + indexAddend) * tempStep);
|
|
75
|
+
indexSatisfied = totalTempDelta >= desiredTotalTempDeltaForIndex;
|
|
76
|
+
indexAddend++;
|
|
77
|
+
}
|
|
78
|
+
lastTemp = temp;
|
|
79
|
+
hueAddend++;
|
|
80
|
+
if (hueAddend > 360) {
|
|
81
|
+
while (allColors.length < divisions) {
|
|
82
|
+
allColors.push(hct);
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const answers = [this.input];
|
|
88
|
+
const increaseHueCount = Math.floor((count - 1) / 2.0);
|
|
89
|
+
for (let i = 1; i < (increaseHueCount + 1); i++) {
|
|
90
|
+
let index = 0 - i;
|
|
91
|
+
while (index < 0) {
|
|
92
|
+
index = allColors.length + index;
|
|
93
|
+
}
|
|
94
|
+
if (index >= allColors.length) {
|
|
95
|
+
index = index % allColors.length;
|
|
96
|
+
}
|
|
97
|
+
answers.splice(0, 0, allColors[index]);
|
|
98
|
+
}
|
|
99
|
+
const decreaseHueCount = count - increaseHueCount - 1;
|
|
100
|
+
for (let i = 1; i < (decreaseHueCount + 1); i++) {
|
|
101
|
+
let index = i;
|
|
102
|
+
while (index < 0) {
|
|
103
|
+
index = allColors.length + index;
|
|
104
|
+
}
|
|
105
|
+
if (index >= allColors.length) {
|
|
106
|
+
index = index % allColors.length;
|
|
107
|
+
}
|
|
108
|
+
answers.push(allColors[index]);
|
|
109
|
+
}
|
|
110
|
+
return answers;
|
|
111
|
+
}
|
|
112
|
+
get complement() {
|
|
113
|
+
if (this.complementCache != null) {
|
|
114
|
+
return this.complementCache;
|
|
115
|
+
}
|
|
116
|
+
const coldestHue = this.coldest.hue;
|
|
117
|
+
const coldestTemp = this.tempsByHct.get(this.coldest);
|
|
118
|
+
const warmestHue = this.warmest.hue;
|
|
119
|
+
const warmestTemp = this.tempsByHct.get(this.warmest);
|
|
120
|
+
const range = warmestTemp - coldestTemp;
|
|
121
|
+
const startHueIsColdestToWarmest = TemperatureCache.isBetween(this.input.hue, coldestHue, warmestHue);
|
|
122
|
+
const startHue = startHueIsColdestToWarmest ? warmestHue : coldestHue;
|
|
123
|
+
const endHue = startHueIsColdestToWarmest ? coldestHue : warmestHue;
|
|
124
|
+
const directionOfRotation = 1.0;
|
|
125
|
+
let smallestError = 1000.0;
|
|
126
|
+
let answer = this.hctsByHue[Math.round(this.input.hue)];
|
|
127
|
+
const complementRelativeTemp = 1.0 - this.inputRelativeTemperature;
|
|
128
|
+
for (let hueAddend = 0.0; hueAddend <= 360.0; hueAddend += 1.0) {
|
|
129
|
+
const hue = mathUtils.sanitizeDegreesDouble(startHue + directionOfRotation * hueAddend);
|
|
130
|
+
if (!TemperatureCache.isBetween(hue, startHue, endHue)) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const possibleAnswer = this.hctsByHue[Math.round(hue)];
|
|
134
|
+
const relativeTemp = (this.tempsByHct.get(possibleAnswer) - coldestTemp) / range;
|
|
135
|
+
const error = Math.abs(complementRelativeTemp - relativeTemp);
|
|
136
|
+
if (error < smallestError) {
|
|
137
|
+
smallestError = error;
|
|
138
|
+
answer = possibleAnswer;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
this.complementCache = answer;
|
|
142
|
+
return this.complementCache;
|
|
143
|
+
}
|
|
144
|
+
relativeTemperature(hct) {
|
|
145
|
+
const range = this.tempsByHct.get(this.warmest) - this.tempsByHct.get(this.coldest);
|
|
146
|
+
const differenceFromColdest = this.tempsByHct.get(hct) - this.tempsByHct.get(this.coldest);
|
|
147
|
+
if (range === 0.0) {
|
|
148
|
+
return 0.5;
|
|
149
|
+
}
|
|
150
|
+
return differenceFromColdest / range;
|
|
151
|
+
}
|
|
152
|
+
get inputRelativeTemperature() {
|
|
153
|
+
if (this.inputRelativeTemperatureCache >= 0.0) {
|
|
154
|
+
return this.inputRelativeTemperatureCache;
|
|
155
|
+
}
|
|
156
|
+
this.inputRelativeTemperatureCache = this.relativeTemperature(this.input);
|
|
157
|
+
return this.inputRelativeTemperatureCache;
|
|
158
|
+
}
|
|
159
|
+
get tempsByHct() {
|
|
160
|
+
if (this.tempsByHctCache.size > 0) {
|
|
161
|
+
return this.tempsByHctCache;
|
|
162
|
+
}
|
|
163
|
+
const allHcts = this.hctsByHue.concat([this.input]);
|
|
164
|
+
const temperaturesByHct = new Map();
|
|
165
|
+
for (const e of allHcts) {
|
|
166
|
+
temperaturesByHct.set(e, TemperatureCache.rawTemperature(e));
|
|
167
|
+
}
|
|
168
|
+
this.tempsByHctCache = temperaturesByHct;
|
|
169
|
+
return temperaturesByHct;
|
|
170
|
+
}
|
|
171
|
+
get hctsByHue() {
|
|
172
|
+
if (this.hctsByHueCache.length > 0) {
|
|
173
|
+
return this.hctsByHueCache;
|
|
174
|
+
}
|
|
175
|
+
const hcts = [];
|
|
176
|
+
for (let hue = 0.0; hue <= 360.0; hue += 1.0) {
|
|
177
|
+
const colorAtHue = Hct.from(hue, this.input.chroma, this.input.tone);
|
|
178
|
+
hcts.push(colorAtHue);
|
|
179
|
+
}
|
|
180
|
+
this.hctsByHueCache = hcts;
|
|
181
|
+
return this.hctsByHueCache;
|
|
182
|
+
}
|
|
183
|
+
static isBetween(angle, a, b) {
|
|
184
|
+
if (a < b) {
|
|
185
|
+
return a <= angle && angle <= b;
|
|
186
|
+
}
|
|
187
|
+
return a <= angle || angle <= b;
|
|
188
|
+
}
|
|
189
|
+
static rawTemperature(color) {
|
|
190
|
+
const lab = colorUtils.labFromArgb(color.toInt());
|
|
191
|
+
const hue = mathUtils.sanitizeDegreesDouble(Math.atan2(lab[2], lab[1]) * 180.0 / Math.PI);
|
|
192
|
+
const chroma = Math.sqrt((lab[1] * lab[1]) + (lab[2] * lab[2]));
|
|
193
|
+
const temperature = -0.5 +
|
|
194
|
+
0.02 * Math.pow(chroma, 1.07) *
|
|
195
|
+
Math.cos(mathUtils.sanitizeDegreesDouble(hue - 50.0) * Math.PI / 180.0);
|
|
196
|
+
return temperature;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import * as mathUtils from "./math_utils.js";
|
|
18
|
+
const SRGB_TO_XYZ = [
|
|
19
|
+
[0.41233895, 0.35762064, 0.18051042],
|
|
20
|
+
[0.2126, 0.7152, 0.0722],
|
|
21
|
+
[0.01932141, 0.11916382, 0.95034478],
|
|
22
|
+
];
|
|
23
|
+
const XYZ_TO_SRGB = [
|
|
24
|
+
[
|
|
25
|
+
3.2413774792388685,
|
|
26
|
+
-1.5376652402851851,
|
|
27
|
+
-0.49885366846268053,
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
-0.9691452513005321,
|
|
31
|
+
1.8758853451067872,
|
|
32
|
+
0.04156585616912061,
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
0.05562093689691305,
|
|
36
|
+
-0.20395524564742123,
|
|
37
|
+
1.0571799111220335,
|
|
38
|
+
],
|
|
39
|
+
];
|
|
40
|
+
const WHITE_POINT_D65 = [95.047, 100.0, 108.883];
|
|
41
|
+
export function argbFromRgb(red, green, blue) {
|
|
42
|
+
return (255 << 24 | (red & 255) << 16 | (green & 255) << 8 | blue & 255) >>>
|
|
43
|
+
0;
|
|
44
|
+
}
|
|
45
|
+
export function argbFromLinrgb(linrgb) {
|
|
46
|
+
const r = delinearized(linrgb[0]);
|
|
47
|
+
const g = delinearized(linrgb[1]);
|
|
48
|
+
const b = delinearized(linrgb[2]);
|
|
49
|
+
return argbFromRgb(r, g, b);
|
|
50
|
+
}
|
|
51
|
+
export function alphaFromArgb(argb) {
|
|
52
|
+
return argb >> 24 & 255;
|
|
53
|
+
}
|
|
54
|
+
export function redFromArgb(argb) {
|
|
55
|
+
return argb >> 16 & 255;
|
|
56
|
+
}
|
|
57
|
+
export function greenFromArgb(argb) {
|
|
58
|
+
return argb >> 8 & 255;
|
|
59
|
+
}
|
|
60
|
+
export function blueFromArgb(argb) {
|
|
61
|
+
return argb & 255;
|
|
62
|
+
}
|
|
63
|
+
export function isOpaque(argb) {
|
|
64
|
+
return alphaFromArgb(argb) >= 255;
|
|
65
|
+
}
|
|
66
|
+
export function argbFromXyz(x, y, z) {
|
|
67
|
+
const matrix = XYZ_TO_SRGB;
|
|
68
|
+
const linearR = matrix[0][0] * x + matrix[0][1] * y + matrix[0][2] * z;
|
|
69
|
+
const linearG = matrix[1][0] * x + matrix[1][1] * y + matrix[1][2] * z;
|
|
70
|
+
const linearB = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z;
|
|
71
|
+
const r = delinearized(linearR);
|
|
72
|
+
const g = delinearized(linearG);
|
|
73
|
+
const b = delinearized(linearB);
|
|
74
|
+
return argbFromRgb(r, g, b);
|
|
75
|
+
}
|
|
76
|
+
export function xyzFromArgb(argb) {
|
|
77
|
+
const r = linearized(redFromArgb(argb));
|
|
78
|
+
const g = linearized(greenFromArgb(argb));
|
|
79
|
+
const b = linearized(blueFromArgb(argb));
|
|
80
|
+
return mathUtils.matrixMultiply([r, g, b], SRGB_TO_XYZ);
|
|
81
|
+
}
|
|
82
|
+
export function argbFromLab(l, a, b) {
|
|
83
|
+
const whitePoint = WHITE_POINT_D65;
|
|
84
|
+
const fy = (l + 16.0) / 116.0;
|
|
85
|
+
const fx = a / 500.0 + fy;
|
|
86
|
+
const fz = fy - b / 200.0;
|
|
87
|
+
const xNormalized = labInvf(fx);
|
|
88
|
+
const yNormalized = labInvf(fy);
|
|
89
|
+
const zNormalized = labInvf(fz);
|
|
90
|
+
const x = xNormalized * whitePoint[0];
|
|
91
|
+
const y = yNormalized * whitePoint[1];
|
|
92
|
+
const z = zNormalized * whitePoint[2];
|
|
93
|
+
return argbFromXyz(x, y, z);
|
|
94
|
+
}
|
|
95
|
+
export function labFromArgb(argb) {
|
|
96
|
+
const linearR = linearized(redFromArgb(argb));
|
|
97
|
+
const linearG = linearized(greenFromArgb(argb));
|
|
98
|
+
const linearB = linearized(blueFromArgb(argb));
|
|
99
|
+
const matrix = SRGB_TO_XYZ;
|
|
100
|
+
const x = matrix[0][0] * linearR + matrix[0][1] * linearG + matrix[0][2] * linearB;
|
|
101
|
+
const y = matrix[1][0] * linearR + matrix[1][1] * linearG + matrix[1][2] * linearB;
|
|
102
|
+
const z = matrix[2][0] * linearR + matrix[2][1] * linearG + matrix[2][2] * linearB;
|
|
103
|
+
const whitePoint = WHITE_POINT_D65;
|
|
104
|
+
const xNormalized = x / whitePoint[0];
|
|
105
|
+
const yNormalized = y / whitePoint[1];
|
|
106
|
+
const zNormalized = z / whitePoint[2];
|
|
107
|
+
const fx = labF(xNormalized);
|
|
108
|
+
const fy = labF(yNormalized);
|
|
109
|
+
const fz = labF(zNormalized);
|
|
110
|
+
const l = 116.0 * fy - 16;
|
|
111
|
+
const a = 500.0 * (fx - fy);
|
|
112
|
+
const b = 200.0 * (fy - fz);
|
|
113
|
+
return [l, a, b];
|
|
114
|
+
}
|
|
115
|
+
export function argbFromLstar(lstar) {
|
|
116
|
+
const y = yFromLstar(lstar);
|
|
117
|
+
const component = delinearized(y);
|
|
118
|
+
return argbFromRgb(component, component, component);
|
|
119
|
+
}
|
|
120
|
+
export function lstarFromArgb(argb) {
|
|
121
|
+
const y = xyzFromArgb(argb)[1];
|
|
122
|
+
return 116.0 * labF(y / 100.0) - 16.0;
|
|
123
|
+
}
|
|
124
|
+
export function yFromLstar(lstar) {
|
|
125
|
+
return 100.0 * labInvf((lstar + 16.0) / 116.0);
|
|
126
|
+
}
|
|
127
|
+
export function lstarFromY(y) {
|
|
128
|
+
return labF(y / 100.0) * 116.0 - 16.0;
|
|
129
|
+
}
|
|
130
|
+
export function linearized(rgbComponent) {
|
|
131
|
+
const normalized = rgbComponent / 255.0;
|
|
132
|
+
if (normalized <= 0.040449936) {
|
|
133
|
+
return normalized / 12.92 * 100.0;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return Math.pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export function delinearized(rgbComponent) {
|
|
140
|
+
const normalized = rgbComponent / 100.0;
|
|
141
|
+
let delinearized = 0.0;
|
|
142
|
+
if (normalized <= 0.0031308) {
|
|
143
|
+
delinearized = normalized * 12.92;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
|
|
147
|
+
}
|
|
148
|
+
return mathUtils.clampInt(0, 255, Math.round(delinearized * 255.0));
|
|
149
|
+
}
|
|
150
|
+
export function whitePointD65() {
|
|
151
|
+
return WHITE_POINT_D65;
|
|
152
|
+
}
|
|
153
|
+
function labF(t) {
|
|
154
|
+
const e = 216.0 / 24389.0;
|
|
155
|
+
const kappa = 24389.0 / 27.0;
|
|
156
|
+
if (t > e) {
|
|
157
|
+
return Math.pow(t, 1.0 / 3.0);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
return (kappa * t + 16) / 116;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function labInvf(ft) {
|
|
164
|
+
const e = 216.0 / 24389.0;
|
|
165
|
+
const kappa = 24389.0 / 27.0;
|
|
166
|
+
const ft3 = ft * ft * ft;
|
|
167
|
+
if (ft3 > e) {
|
|
168
|
+
return ft3;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
return (116 * ft - 16) / kappa;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2021 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { QuantizerCelebi } from "../quantize/quantizer_celebi.js";
|
|
18
|
+
import { Score } from "../score/score.js";
|
|
19
|
+
import { argbFromRgb } from "./color_utils.js";
|
|
20
|
+
export async function sourceColorFromImage(image) {
|
|
21
|
+
const imageBytes = await new Promise((resolve, reject) => {
|
|
22
|
+
const canvas = document.createElement('canvas');
|
|
23
|
+
const context = canvas.getContext('2d');
|
|
24
|
+
if (!context) {
|
|
25
|
+
reject(new Error('Could not get canvas context'));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const loadCallback = () => {
|
|
29
|
+
canvas.width = image.width;
|
|
30
|
+
canvas.height = image.height;
|
|
31
|
+
context.drawImage(image, 0, 0);
|
|
32
|
+
let rect = [0, 0, image.width, image.height];
|
|
33
|
+
const area = image.dataset['area'];
|
|
34
|
+
if (area && /^\d+(\s*,\s*\d+){3}$/.test(area)) {
|
|
35
|
+
rect = area.split(/\s*,\s*/).map(s => {
|
|
36
|
+
return parseInt(s, 10);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const [sx, sy, sw, sh] = rect;
|
|
40
|
+
resolve(context.getImageData(sx, sy, sw, sh).data);
|
|
41
|
+
};
|
|
42
|
+
const errorCallback = () => {
|
|
43
|
+
reject(new Error('Image load failed'));
|
|
44
|
+
};
|
|
45
|
+
if (image.complete) {
|
|
46
|
+
loadCallback();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
image.onload = loadCallback;
|
|
50
|
+
image.onerror = errorCallback;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return sourceColorFromImageBytes(imageBytes);
|
|
54
|
+
}
|
|
55
|
+
export function sourceColorFromImageBytes(imageBytes) {
|
|
56
|
+
const pixels = [];
|
|
57
|
+
for (let i = 0; i < imageBytes.length; i += 4) {
|
|
58
|
+
const r = imageBytes[i];
|
|
59
|
+
const g = imageBytes[i + 1];
|
|
60
|
+
const b = imageBytes[i + 2];
|
|
61
|
+
const a = imageBytes[i + 3];
|
|
62
|
+
if (a < 255) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const argb = argbFromRgb(r, g, b);
|
|
66
|
+
pixels.push(argb);
|
|
67
|
+
}
|
|
68
|
+
const result = QuantizerCelebi.quantize(pixels, 128);
|
|
69
|
+
const ranked = Score.score(result);
|
|
70
|
+
const top = ranked[0];
|
|
71
|
+
return top;
|
|
72
|
+
}
|