TinyColor
TinyColor copied to clipboard
Decimals are rounded (`rgb(...) -> toRgb`)
This code produces rounded output:
tinycolor('rgb(255, 25.5, 0)').toRgb();
// {r: 255, g: 26, b: 0, a: 1}
25.5 was rounded to 26.
Is the rounding intentional?
See this PR which adds a failing unit test: https://github.com/bgrins/TinyColor/pull/277
The code looks like it's rounding intentionally: https://github.com/bgrins/TinyColor/blob/13851a7f4950040d9ad8557c3a92d9f4d8d02843/tinycolor.js#L132-L139
Just curious, what's the reason for rounding? In CSS, decimal numbers are valid. Is there another concern?
Would you be open to either removing the rounding or adding a toRgbValues function? There is no existing function that just gets the rgb values.
My use case is data transformation. I'm getting data from a server where the colors are in an RGB object format. I transform them into CSS colors for the web UI, where a user can edit the colors and save them back to the server. So the pipeline is rgba values -> rgba string -> rgba values. In this case it's ideal that the colors remain as-is and not be rounded.
Ah, I see not all older browsers support float values in rgb expressions: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb#browser_compatibility
Is a PR welcome for implementing toRgbValues? I've adjusted my MR to implement that.
Reasoning: RGB values can be used for other things besides CSS expressions, so there's value in preserving decimal places.
Anyone here?