diff --git a/dist/frappe-charts.esm.js b/dist/frappe-charts.esm.js index d701d5f..a90c06d 100644 --- a/dist/frappe-charts.esm.js +++ b/dist/frappe-charts.esm.js @@ -448,7 +448,7 @@ function getSplineCurvePointsStr(xList, yList) { angle: Math.atan2(lengthY, lengthX) }; }; - + let controlPoint = (current, previous, next, reverse) => { let p = previous || current; let n = next || current; @@ -459,19 +459,19 @@ function getSplineCurvePointsStr(xList, yList) { let y = current[1] + Math.sin(angle) * length; return [x, y]; }; - + let bezierCommand = (point, i, a) => { let cps = controlPoint(a[i - 1], a[i - 2], point); let cpe = controlPoint(point, a[i - 1], a[i + 1], true); return `C ${cps[0]},${cps[1]} ${cpe[0]},${cpe[1]} ${point[0]},${point[1]}`; }; - + let pointStr = (points, command) => { return points.reduce((acc, point, i, a) => i === 0 ? `${point[0]},${point[1]}` : `${acc} ${command(point, i, a)}`, ''); }; - + return pointStr(points, bezierCommand); } @@ -520,6 +520,12 @@ function isValidColor(string) { } 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; }; diff --git a/src/js/utils/colors.js b/src/js/utils/colors.js index 0aa967e..b77b6e0 100644 --- a/src/js/utils/colors.js +++ b/src/js/utils/colors.js @@ -43,5 +43,11 @@ export function isValidColor(string) { } 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; }; diff --git a/src/js/utils/test/colors.test.js b/src/js/utils/test/colors.test.js new file mode 100644 index 0000000..5a76231 --- /dev/null +++ b/src/js/utils/test/colors.test.js @@ -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')); + }); +}); \ No newline at end of file diff --git a/src/js/utils/test/helpers.test.js b/src/js/utils/test/helpers.test.js index 2e646f1..0d8e7e3 100644 --- a/src/js/utils/test/helpers.test.js +++ b/src/js/utils/test/helpers.test.js @@ -1,10 +1,10 @@ -const assert = require('assert') -const helpers = require('../helpers') +const assert = require('assert'); +const helpers = require('../helpers'); describe('utils.helpers', () => { - it('should return a value fixed upto 2 decimals', () => { - assert.equal(helpers.floatTwo(1.234), 1.23); - assert.equal(helpers.floatTwo(1.456), 1.46); - assert.equal(helpers.floatTwo(1), 1.00); - }); + it('should return a value fixed upto 2 decimals', () => { + assert.equal(helpers.floatTwo(1.234), 1.23); + assert.equal(helpers.floatTwo(1.456), 1.46); + assert.equal(helpers.floatTwo(1), 1.00); + }); }); \ No newline at end of file