- Adjust getColor to support RGB (TODO: HSL colors)
- Add Test to verify changes
This commit is contained in:
parent
f644e19a5b
commit
869c747be1
6
dist/frappe-charts.esm.js
vendored
6
dist/frappe-charts.esm.js
vendored
@ -520,6 +520,12 @@ function isValidColor(string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getColor = (color) => {
|
const getColor = (color) => {
|
||||||
|
// When RGB color, convert to hexadecimal (alpha value is omitted)
|
||||||
|
if((/rgb[a]{0,1}\([\d, ]+\)/gim).test(color)) {
|
||||||
|
return (/\D+(\d*)\D+(\d*)\D+(\d*)/gim).exec(color)
|
||||||
|
.map((x, i) => (i !== 0 ? Number(x).toString(16) : '#'))
|
||||||
|
.reduce((c, ch) => `${c}${ch}`);
|
||||||
|
}
|
||||||
return PRESET_COLOR_MAP[color] || color;
|
return PRESET_COLOR_MAP[color] || color;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -43,5 +43,11 @@ export function isValidColor(string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getColor = (color) => {
|
export const getColor = (color) => {
|
||||||
|
// When RGB color, convert to hexadecimal (alpha value is omitted)
|
||||||
|
if((/rgb[a]{0,1}\([\d, ]+\)/gim).test(color)) {
|
||||||
|
return (/\D+(\d*)\D+(\d*)\D+(\d*)/gim).exec(color)
|
||||||
|
.map((x, i) => (i !== 0 ? Number(x).toString(16) : '#'))
|
||||||
|
.reduce((c, ch) => `${c}${ch}`);
|
||||||
|
}
|
||||||
return PRESET_COLOR_MAP[color] || color;
|
return PRESET_COLOR_MAP[color] || color;
|
||||||
};
|
};
|
||||||
|
|||||||
14
src/js/utils/test/colors.test.js
Normal file
14
src/js/utils/test/colors.test.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
const assert = require('assert');
|
||||||
|
const colors = require('../colors');
|
||||||
|
|
||||||
|
describe('utils.colors', () => {
|
||||||
|
it('should return #aaabac for RGB()', () => {
|
||||||
|
assert.equal(colors.getColor('rgb(170, 171, 172)', '#aaabac'));
|
||||||
|
});
|
||||||
|
it('should return #ff5858 for the named color red', () => {
|
||||||
|
assert.equal(colors.getColor('red', '#ff5858d'));
|
||||||
|
});
|
||||||
|
it('should return #1a5c29 for the hex color #1a5c29', () => {
|
||||||
|
assert.equal(colors.getColor('#1a5c29', '#1a5c29'));
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,5 +1,5 @@
|
|||||||
const assert = require('assert')
|
const assert = require('assert');
|
||||||
const helpers = require('../helpers')
|
const helpers = require('../helpers');
|
||||||
|
|
||||||
describe('utils.helpers', () => {
|
describe('utils.helpers', () => {
|
||||||
it('should return a value fixed upto 2 decimals', () => {
|
it('should return a value fixed upto 2 decimals', () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user