Skip to content

Commit 25e88fb

Browse files
ethansharInbal-Tish
authored andcommitted
improve getColorTint util to support custom colors tints (#257)
* add fallback logic for getColorTint to support random colors tints * improve logic of getColorTint for bright tint colors * improve getColorTint util to support custom colors tints * handle edge cases for getColorTint
1 parent 46d57a8 commit 25e88fb

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"babel-plugin-transform-inline-environment-variables": "^0.0.2",
4040
"hoist-non-react-statics": "^2.3.0",
4141
"lodash": "^4.0.0",
42+
"onecolor": "^3.1.0",
4243
"prop-types": "^15.5.10",
4344
"react-native-animatable": "^1.1.0",
4445
"react-native-blur": "^3.1.1",

src/style/__tests__/colors.spec.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,35 @@ describe('services/AvatarService', () => {
4545
expect(uut.getColorTint(uut.blue20, 60)).toEqual(uut.blue60);
4646
});
4747

48-
/* it('should throw error if the color could not be found', () => {
49-
expect(() => uut.getColorTint('#ff115', '10')).toThrow(new Error('"Colors.getColorTint" could not find this color'));
48+
it('should return same color if tintLevel param is undefined or NaN', () => {
49+
expect(uut.getColorTint('#F1BE0B')).toEqual('#F1BE0B');
50+
expect(uut.getColorTint('#F1BE0B', '2a4')).toEqual('#F1BE0B');
5051
});
5152

52-
it('should throw error if tint key was not provided', () => {
53-
expect(() => uut.getColorTint('#ff115')).toThrow(new Error('"Colors.getColorTint" must accept a color and tintKey params'));
54-
}); */
53+
it('should return undefined if color param is undefined', () => {
54+
expect(uut.getColorTint(undefined, 10)).toEqual(undefined);
55+
});
56+
57+
it('should handle color that does not exist in uilib', () => {
58+
expect(uut.getColorTint('#F1BE0B', 10)).toEqual('#b28c08');
59+
expect(uut.getColorTint('#F1BE0B', 20)).toEqual('#ca9f09');
60+
expect(uut.getColorTint('#F1BE0B', 30)).toEqual('#F1BE0B');
61+
expect(uut.getColorTint('#F1BE0B', 40)).toEqual('#f5d04d');
62+
expect(uut.getColorTint('#F1BE0B', 50)).toEqual('#f9e291');
63+
expect(uut.getColorTint('#F1BE0B', 60)).toEqual('#fbedbb');
64+
expect(uut.getColorTint('#F1BE0B', 70)).toEqual('#fdf4d6');
65+
expect(uut.getColorTint('#F1BE0B', 80)).toEqual('#fef9e7');
66+
});
67+
68+
it('should round down tint level to the nearest one', () => {
69+
expect(uut.getColorTint('#F1BE0B', 75)).toEqual('#fdf4d6');
70+
expect(uut.getColorTint('#F1BE0B', 25)).toEqual('#ca9f09');
71+
expect(uut.getColorTint('#F1BE0B', 35)).toEqual('#F1BE0B');
72+
});
73+
74+
it('should handle out of range tint levels and round them to the nearest one in range', () => {
75+
expect(uut.getColorTint('#F1BE0B', 3)).toEqual('#b28c08');
76+
expect(uut.getColorTint('#F1BE0B', 95)).toEqual('#fef9e7');
77+
});
5578
});
5679
});

src/style/colors.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import _ from 'lodash';
22
import {colorsPalette} from './colorsPalette';
33

4+
const one = require('onecolor');
5+
46
class Colors {
57
/**
68
* Load custom set of colors
@@ -60,10 +62,15 @@ class Colors {
6062
}
6163

6264
getColorTint(color, tintKey) {
65+
const BASE_COLOR_LEVEL = 3;
66+
const darkRatios = [0.13, 0.08];
67+
const lightRatios = [0.27, 0.55, 0.72, 0.83, 0.9];
68+
6369
const colorKey = _.findKey(this, (value, key) => this[key] === color);
6470

65-
if (_.isUndefined(tintKey)) {
66-
throw new Error('"Colors.getColorTint" must accept a color and tintKey params');
71+
if (_.isUndefined(tintKey) || isNaN(tintKey) || _.isUndefined(color)) {
72+
console.error('"Colors.getColorTint" must accept a color and tintKey params');
73+
return color;
6774
}
6875

6976
if (colorKey) {
@@ -74,10 +81,20 @@ class Colors {
7481
return color;
7582
}
7683
return requiredColor;
84+
// Handles dynamic colors (non uilib colors)
7785
} else {
78-
// throw new Error('"Colors.getColorTint" could not find this color');
79-
console.warn('"Colors.getColorTint" could not find this color');
80-
return color;
86+
let tintLevel = Math.floor(Number(tintKey) / 10);
87+
tintLevel = Math.max(1, tintLevel);
88+
tintLevel = Math.min(8, tintLevel);
89+
if (tintLevel === BASE_COLOR_LEVEL) {
90+
return color;
91+
} else if (tintLevel <= BASE_COLOR_LEVEL) {
92+
const darkRatio = darkRatios[tintLevel - 1];
93+
return one(color).darken(darkRatio).hex();
94+
} else {
95+
const lightRatio = lightRatios[tintLevel - 4];
96+
return one(color).mix('#ffffff', lightRatio).hex();
97+
}
8198
}
8299
}
83100
}

0 commit comments

Comments
 (0)