From 0c3e2d52978861daaa74d371d533e3850d9e15df Mon Sep 17 00:00:00 2001 From: Mark McDowell Date: Wed, 11 Sep 2019 19:31:58 +0100 Subject: [PATCH] fix(colors): alpha channel is now used if rgba is used Previously the alpha channel was ignored and the opacity setting was used instead. Now the alpha channel will be used if specified. Closes #27 --- packages/charts/src/utils/colors.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/charts/src/utils/colors.ts b/packages/charts/src/utils/colors.ts index 0c70c4392..07c674cb6 100644 --- a/packages/charts/src/utils/colors.ts +++ b/packages/charts/src/utils/colors.ts @@ -1,11 +1,15 @@ import { colorPresets } from "./colorPresets"; export const colorToRGBA = (inputColor: string, opacity: number = 1) => { - if (inputColor.charAt(0) === "#") { + if (inputColor.startsWith("#")) { return hexToRGBA(inputColor.trim(), opacity); } - if (inputColor.indexOf("rgb(") !== -1 || inputColor.indexOf("rgba(") !== -1) { + if (inputColor.startsWith("rgba")) { + return inputColor; + } + + if (inputColor.startsWith("rgb")) { return rgbToRGBA(inputColor.trim(), opacity); } @@ -31,6 +35,7 @@ export const rgbToRGBA = (inputRGB: string, opacity: number = 1) => { throw new Error(`invalid inputRGB: ${inputRGB}`); } const [, r, g, b] = res; + return `rgba(${r}, ${g}, ${b}, ${opacity})`; };