Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimals are rounded (rgb(...) -> toRgb) #278

Open
matthias-ccri opened this issue Jun 26, 2024 · 3 comments
Open

Decimals are rounded (rgb(...) -> toRgb) #278

matthias-ccri opened this issue Jun 26, 2024 · 3 comments

Comments

@matthias-ccri
Copy link

matthias-ccri commented Jun 26, 2024

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: #277

@matthias-ccri
Copy link
Author

The code looks like it's rounding intentionally:

TinyColor/tinycolor.js

Lines 132 to 139 in 13851a7

toRgb: function toRgb() {
return {
r: Math.round(this._r),
g: Math.round(this._g),
b: Math.round(this._b),
a: this._a
};
},

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.

@matthias-ccri
Copy link
Author

matthias-ccri commented Jun 26, 2024

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.

@matthias-ccri
Copy link
Author

Anyone here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant