Skip to content

Commit 14643b4

Browse files
authored
getSystemColorByHex now have options object with validColors (#2363)
1 parent 8f2a148 commit 14643b4

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/style/__tests__/colors.spec.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import uut from '../colors';
22
const SYSTEM_COLORS = ['grey', 'white', 'black'];
3+
const GetColorsByHexOptions = {validColors: SYSTEM_COLORS};
34

45
describe('style/Colors', () => {
56
it('should add alpha to hex color value', () => {
@@ -194,15 +195,15 @@ describe('style/Colors', () => {
194195

195196
describe('getSystemColorByHex', () => {
196197
it('should return the system color, if the color is included in the validColors array', () => {
197-
expect(uut.getSystemColorByHex('#FFFFFF', SYSTEM_COLORS)).toEqual('white');
198-
expect(uut.getSystemColorByHex('#000000', SYSTEM_COLORS)).toEqual('black');
199-
expect(uut.getSystemColorByHex('#116DFF', [...SYSTEM_COLORS, 'blue'])).toEqual('blue30');
200-
expect(uut.getSystemColorByHex('#FB6413', [...SYSTEM_COLORS, 'orange'])).toEqual('orange30');
198+
expect(uut.getSystemColorByHex('#FFFFFF', GetColorsByHexOptions)).toEqual('white');
199+
expect(uut.getSystemColorByHex('#000000', GetColorsByHexOptions)).toEqual('black');
200+
expect(uut.getSystemColorByHex('#116DFF', {validColors: [...SYSTEM_COLORS, 'blue']})).toEqual('blue30');
201+
expect(uut.getSystemColorByHex('#FB6413', {validColors: [...SYSTEM_COLORS, 'orange']})).toEqual('orange30');
201202
});
202203

203204
it('should return undefined if the color is not included in validColors', () => {
204-
expect(uut.getSystemColorByHex('#116DFF', SYSTEM_COLORS)).toEqual(undefined);
205-
expect(uut.getSystemColorByHex('#00A87E', SYSTEM_COLORS)).toEqual(undefined);
205+
expect(uut.getSystemColorByHex('#116DFF', GetColorsByHexOptions)).toEqual(undefined);
206+
expect(uut.getSystemColorByHex('#00A87E', GetColorsByHexOptions)).toEqual(undefined);
206207
});
207208

208209
it('without validColors array the function will return the first system color name match the color', () => {
@@ -212,8 +213,8 @@ describe('style/Colors', () => {
212213

213214
it('should return undefined for color that does not exist in our colors palette.', () => {
214215
expect(uut.getSystemColorByHex('#5A48F5')).toEqual('violet30');
215-
expect(uut.getSystemColorByHex('#5A48F5', SYSTEM_COLORS)).toEqual(undefined);
216-
expect(uut.getSystemColorByHex('#5A48F5', ['primary'])).toEqual('primary');
216+
expect(uut.getSystemColorByHex('#5A48F5', GetColorsByHexOptions)).toEqual(undefined);
217+
expect(uut.getSystemColorByHex('#5A48F5', {validColors: [...SYSTEM_COLORS, 'primary']})).toEqual('primary');
217218
});
218219
});
219220
});

src/style/colors.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Scheme, {Schemes, SchemeType} from './scheme';
1313
export type DesignToken = {semantic?: [string]; resource_paths?: [string]; toString: Function};
1414
export type TokensOptions = {primaryColor: string};
1515
export type GetColorTintOptions = {avoidReverseOnDark?: boolean};
16+
export type GetColorByHexOptions = {validColors?: string[]};
1617

1718
export class Colors {
1819
[key: string]: any;
@@ -146,9 +147,10 @@ export class Colors {
146147
return ColorName.name(color)[1];
147148
}
148149

149-
getSystemColorByHex(colorValue: string, validColors?: string[]) {
150+
getSystemColorByHex(colorValue: string, options: GetColorByHexOptions = {}) {
150151
const color = colorStringValue(colorValue);
151152
const results: string[] = [];
153+
const validColors = options?.validColors;
152154

153155
for (const [key, value] of Object.entries(this)) {
154156
if (value.toString() === color) {

0 commit comments

Comments
 (0)