Skip to content

improve getColorTint util to support custom colors tints #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"babel-plugin-transform-inline-environment-variables": "^0.0.2",
"hoist-non-react-statics": "^2.3.0",
"lodash": "^4.0.0",
"onecolor": "^3.1.0",
"prop-types": "^15.5.10",
"react-native-animatable": "^1.1.0",
"react-native-blur": "^3.1.1",
Expand Down
33 changes: 28 additions & 5 deletions src/style/__tests__/colors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,35 @@ describe('services/AvatarService', () => {
expect(uut.getColorTint(uut.blue20, 60)).toEqual(uut.blue60);
});

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

it('should throw error if tint key was not provided', () => {
expect(() => uut.getColorTint('#ff115')).toThrow(new Error('"Colors.getColorTint" must accept a color and tintKey params'));
}); */
it('should return undefined if color param is undefined', () => {
expect(uut.getColorTint(undefined, 10)).toEqual(undefined);
});

it('should handle color that does not exist in uilib', () => {
expect(uut.getColorTint('#F1BE0B', 10)).toEqual('#b28c08');
expect(uut.getColorTint('#F1BE0B', 20)).toEqual('#ca9f09');
expect(uut.getColorTint('#F1BE0B', 30)).toEqual('#F1BE0B');
expect(uut.getColorTint('#F1BE0B', 40)).toEqual('#f5d04d');
expect(uut.getColorTint('#F1BE0B', 50)).toEqual('#f9e291');
expect(uut.getColorTint('#F1BE0B', 60)).toEqual('#fbedbb');
expect(uut.getColorTint('#F1BE0B', 70)).toEqual('#fdf4d6');
expect(uut.getColorTint('#F1BE0B', 80)).toEqual('#fef9e7');
});

it('should round down tint level to the nearest one', () => {
expect(uut.getColorTint('#F1BE0B', 75)).toEqual('#fdf4d6');
expect(uut.getColorTint('#F1BE0B', 25)).toEqual('#ca9f09');
expect(uut.getColorTint('#F1BE0B', 35)).toEqual('#F1BE0B');
});

it('should handle out of range tint levels and round them to the nearest one in range', () => {
expect(uut.getColorTint('#F1BE0B', 3)).toEqual('#b28c08');
expect(uut.getColorTint('#F1BE0B', 95)).toEqual('#fef9e7');
});
});
});
27 changes: 22 additions & 5 deletions src/style/colors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import _ from 'lodash';
import {colorsPalette} from './colorsPalette';

const one = require('onecolor');

class Colors {
/**
* Load custom set of colors
Expand Down Expand Up @@ -60,10 +62,15 @@ class Colors {
}

getColorTint(color, tintKey) {
const BASE_COLOR_LEVEL = 3;
const darkRatios = [0.13, 0.08];
const lightRatios = [0.27, 0.55, 0.72, 0.83, 0.9];

const colorKey = _.findKey(this, (value, key) => this[key] === color);

if (_.isUndefined(tintKey)) {
throw new Error('"Colors.getColorTint" must accept a color and tintKey params');
if (_.isUndefined(tintKey) || isNaN(tintKey) || _.isUndefined(color)) {
console.error('"Colors.getColorTint" must accept a color and tintKey params');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't return here you might use the undefined 'tintKey' later

return color;
}

if (colorKey) {
Expand All @@ -74,10 +81,20 @@ class Colors {
return color;
}
return requiredColor;
// Handles dynamic colors (non uilib colors)
} else {
// throw new Error('"Colors.getColorTint" could not find this color');
console.warn('"Colors.getColorTint" could not find this color');
return color;
let tintLevel = Math.floor(Number(tintKey) / 10);
tintLevel = Math.max(1, tintLevel);
tintLevel = Math.min(8, tintLevel);
if (tintLevel === BASE_COLOR_LEVEL) {
return color;
} else if (tintLevel <= BASE_COLOR_LEVEL) {
const darkRatio = darkRatios[tintLevel - 1];
return one(color).darken(darkRatio).hex();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if 'color' is undefined? Won't it crash here?

} else {
const lightRatio = lightRatios[tintLevel - 4];
return one(color).mix('#ffffff', lightRatio).hex();
}
}
}
}
Expand Down