TinyColor icon indicating copy to clipboard operation
TinyColor copied to clipboard

Added .toRgbaString() method to tinycolor.js

Open haoliu119 opened this issue 12 years ago • 1 comments

needed a method to convert to rgba(....) string regardless whether alpha is 1 or not.

haoliu119 avatar Oct 03 '13 10:10 haoliu119

I'm not sure about exposing a new method for this, since we would then need to also expose toHslaString, toHsvaString, etc for consistency. I think maybe we could add an option to pass "rgba" to the standard toString() method and have that force alpha. It may be best to have a private function called toRgbString that takes in r, g, b, a, and an extra param called forceAlpha that does what the exposed method currently does. Then have the exposed toRgbString just pass the properties along, while this new mode (toString("rgba")) would pass in (r, g, b, a, true). Does this make sense? Something like this, maybe:

function toRgbString(r, b, b, a, forceAlpha) {
    return (a === 1 && !forceAlpha) ?
      "rgb("  + mathRound(r) + ", " + mathRound(g) + ", " + mathRound(b) + ")" :
      "rgba(" + mathRound(r) + ", " + mathRound(g) + ", " + mathRound(b) + ", " + roundA + ")";
}

bgrins avatar Oct 13 '13 16:10 bgrins