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

.toRgbString() returns unexpectedly unspaced result #88

Closed
jhack89 opened this issue Jan 14, 2016 · 9 comments
Closed

.toRgbString() returns unexpectedly unspaced result #88

jhack89 opened this issue Jan 14, 2016 · 9 comments
Milestone

Comments

@jhack89
Copy link

jhack89 commented Jan 14, 2016

Dear Developers,

color.toRgbString() is supposed to be compatible with the jQuery.css(), however it does not return an easily comparable value.
In fact, jQuery.css("color") returns colors as "rgb(xxx, xxx, xxx)" while .toRgbString() return the same as "rgb(xxx,xxx,xxx)" without spaces! This makes impossible to easily compare colors as strings without workarounds such as $("<div>").css("color", color.toRgbString()).css("color"). This is especially important when comparing miscellaneous CSS proprieties.

Example code where I needed to use the workaround to make things work seamlessly:

$("#element").click(function() {
$(this).animate(
{
   "backgroundColor": "red",
   "width", "+=100"
}, {
    duration: 1000,
    step(val, fx) {
    // some code here
    if( $("<div>").css(fx.prop, fx.end.toString()).css(fx.prop) == check[fx.prop]) { ... }
    /* instead of the expected: 
     * if(fx.end.toString() == check[fx.prop]) { ... }
     */
    // more code here
    }
})
});

It would be great if this little bug could be fixed.
Thank you for your great work,
Warmest Regards.

@dmethvin
Copy link
Member

There is no conversion of units or format with the value that jQuery's .css() returns, so you are at the mercy of whatever the browser provides for color units. I don't know if that's a problem of the past that just affects browsers like IE8, or whether there are differences in newer ones as well. Still, it does make sense to match the browser format including spaces. Do you want to make a pull request?

@gnarf
Copy link
Member

gnarf commented Jan 14, 2016

Hrm.

So the interesting part about this, is there is nothing in the spec that specifies the spacing, or format of this value coming back from the style object. Browser A might return rgba values as percents while browser b uses integer format.

I could see making the .toString() use a style property to calculate it's return value, where the .toRgbString etc methods would continue to return what they do.

@dmethvin
Copy link
Member

I agree it's not specified by any standard, and we definitely saw differences in the past. What seems to have happened over the years is that everyone gravitated towards the rgb(...) format for the colors returned by getComputedStyle strings. That's why I cautioned @jhack89 about depending on it always being that way. Still, it seems like a really trivial change to make it match. I can see that being a breaking change to code that expects/parses the current format though.

@gnarf
Copy link
Member

gnarf commented Jan 14, 2016

Interestingly, .toString is currently undocumented, and has a weird transparent case:

toString: function() {
return this._rgba[ 3 ] === 0 ? "transparent" : this.toRgbaString();
}

Though I could ALSO see this being a breaking change, perhaps color 3.0 is on the horizon? Updated for jQuery 1.8 animation, removing IE6, etc?

@gnarf
Copy link
Member

gnarf commented Jan 14, 2016

Though I'd also like to point out that the OP could use fx.end.is( check[ fx.prop ] ) instead of casting strings.

@dmethvin
Copy link
Member

Seems like the "right" solution TBH.

@mgol
Copy link
Member

mgol commented Jan 31, 2018

The spec seems to have evolved since the initial report and now it's pretty strict in the expected output of computed style's color formatting, i.e. it requires the rgb form (or rgba if alpha is non-1) and it requires the values to be joined by , , i.e. with a space after the comma. See:
https://drafts.csswg.org/cssom/#serializing-css-values

@mgol
Copy link
Member

mgol commented Jan 31, 2018

That also means transparent is translated to rgba(0, 0, 0, 0). Should we align with the standard for 3.0.0?

@mgol mgol added this to the 3.0.0 milestone Jan 31, 2018
@dmethvin
Copy link
Member

That seems like a good idea.

@mgol mgol closed this as completed in aaf03cc May 6, 2020
mgol added a commit to jquery/jquery-ui that referenced this issue May 15, 2024
Breaking changes applicable to jQuery UI:
* Use a space when serializing, remove the transparent case ([#88](jquery/jquery-color#88), [aaf03cc](jquery/jquery-color@aaf03cc))

See https://github.com/jquery/jquery-color/releases/tag/3.0.0 for more
information.

Fixes gh-2240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants