Skip to content

Commit f1ea3e0

Browse files
authored
Support retrieving color JS value (#1956)
1 parent 7eebdf1 commit f1ea3e0

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/style/colors.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ export class Colors {
129129
}
130130
}
131131

132+
/**
133+
* Return the original color string value by its colorKey based on the current scheme(light/dark)
134+
*/
135+
getColorValue(colorKey: string) {
136+
return Scheme.getScheme(true)[colorKey] ?? this[colorKey];
137+
}
138+
139+
getColorName(color: string) {
140+
return ColorName.name(color)[1];
141+
}
142+
132143
getColorTint(color: string, tintKey: string | number) {
133144
if (_.isUndefined(tintKey) || isNaN(tintKey as number) || _.isUndefined(color)) {
134145
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
@@ -159,10 +170,6 @@ export class Colors {
159170
return this.getTintedColorForDynamicHex(color, tintKey);
160171
}
161172

162-
getColorName(color: string) {
163-
return ColorName.name(color)[1];
164-
}
165-
166173
getTintedColorForDynamicHex(color: string, tintKey: string | number) {
167174
// Handles dynamic colors (non uilib colors)
168175
let tintLevel = Math.floor(Number(tintKey) / 10);

src/style/scheme.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ export type SchemeType = 'default' | 'light' | 'dark';
77
export type SchemeChangeListener = (schemeType?: 'light' | 'dark') => void;
88

99
class Scheme {
10-
currentScheme: SchemeType = 'default';
11-
schemes: Schemes = {light: {}, dark: {}};
12-
changeListeners: SchemeChangeListener[] = [];
10+
private currentScheme: SchemeType = 'default';
11+
private schemes: Schemes = {light: {}, dark: {}};
12+
private schemesJS: Schemes = {light: {}, dark: {}};
13+
private changeListeners: SchemeChangeListener[] = [];
1314
private usePlatformColors = false;
1415

1516
constructor() {
@@ -63,8 +64,9 @@ class Scheme {
6364
throw new Error(`There is a mismatch in scheme keys: ${missingKeys.join(', ')}`);
6465
}
6566

66-
const platformColorsSchemes: Schemes = cloneDeep(schemes);
67+
merge(this.schemesJS, schemes);
6768

69+
const platformColorsSchemes: Schemes = cloneDeep(schemes);
6870
forEach(schemes, (scheme, schemeKey) => {
6971
forEach(scheme, (colorValue, colorKey) => {
7072
// @ts-expect-error
@@ -84,15 +86,18 @@ class Scheme {
8486
});
8587
});
8688
});
87-
8889
merge(this.schemes, platformColorsSchemes);
8990
}
9091

9192
/**
9293
* Retrieve scheme by current scheme type
9394
*/
94-
getScheme() {
95-
return this.schemes[this.getSchemeType()];
95+
getScheme(useJS = false) {
96+
if (useJS) {
97+
return this.schemesJS[this.getSchemeType()];
98+
} else {
99+
return this.schemes[this.getSchemeType()];
100+
}
96101
}
97102

98103
/**

0 commit comments

Comments
 (0)