This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 12824 - This doesn't say how to serialize non-zero alpha values: how many digits should be used? The correct answer is the minimum number necessary so that the resulting string evaluates to the same floating-point number as you started with.
Summary: This doesn't say how to serialize non-zero alpha values: how many digits shou...
Status: RESOLVED FIXED
Alias: None
Product: HTML WG
Classification: Unclassified
Component: LC1 HTML Canvas 2D Context (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal
Target Milestone: ---
Assignee: Ian 'Hixie' Hickson
QA Contact: HTML WG Bugzilla archive list
URL: http://www.whatwg.org/specs/web-apps/...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-30 22:11 UTC by contributor
Modified: 2011-08-10 22:34 UTC (History)
6 users (show)

See Also:


Attachments
Testcase (485 bytes, text/html)
2011-05-31 02:10 UTC, Boris Zbarsky
Details

Description contributor 2011-05-30 22:11:46 UTC
Specification: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
Section: http://www.whatwg.org/specs/web-apps/current-work/complete.html#colors-and-styles

Comment:
This doesn't say how to serialize non-zero alpha values: how many digits
should be used?  The correct answer is the minimum number necessary so that
the resulting string evaluates to the same floating-point number as you
started with.

Posted from: 68.175.61.233
User agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.772.0 Safari/535.1
Comment 1 Boris Zbarsky 2011-05-31 02:09:56 UTC
Actually, the correct answer may be to use however many are needed to ensure that your round-trip back to the same value you have now if you parse the result.

For example, consider a UA that stores alpha internally as an integer in the range [0, 255] (a very common thing to do; Presto, WebKit nightly, and Gecko all do it).  It most certainly cannot produce the original floating-point number when asked.  What it _can_ produce is a floating-point number which when parsed and converted to an integer in that range will produce the integer you start with.  In practice that means either 2 or 3 digits of precision.

I agree that this should be specified.

I'll attach a simple testcase that tests what browsers do right now for alpha values of 0 to 1 in steps of 0.001; it shows that Presto, Gecko, and WebKit all step up their serialized value only once every 0.004 or so, which means they're storing 250-ish distinct values, as I said above.

Note that of the three Gecko is the only one that even tries to make the serialized value look "nice" (in the sense of using the smallest number of digits that will round-trip to the same integer; this means that 0.1 ends up serialized as "0.1" and not "0.09804" (Chrome) or "0.10196" (Presto and Safari)).
Comment 2 Boris Zbarsky 2011-05-31 02:10:21 UTC
Created attachment 991 [details]
Testcase
Comment 3 Aryeh Gregor 2011-05-31 14:20:49 UTC
(In reply to comment #1)
> Actually, the correct answer may be to use however many are needed to ensure
> that your round-trip back to the same value you have now if you parse the
> result.

Good catch.

> For example, consider a UA that stores alpha internally as an integer in the
> range [0, 255] (a very common thing to do; Presto, WebKit nightly, and Gecko
> all do it).

Should this be standardized?  It has some author-visible implications.  To start with, if alpha values are serialized as you suggest, setting and then reading back strokeStyle or fillStyle will give you a different number of digits in different UAs for the alpha, depending on how much internal precision they have.

More than that, presumably it will also be detectable by drawing some non-opaque things on top of each other and measuring the exact color of the result.  If everything gets clamped to 8-bit integers, the rounding will be different from if you use actual floats.

> Note that of the three Gecko is the only one that even tries to make the
> serialized value look "nice" (in the sense of using the smallest number of
> digits that will round-trip to the same integer; this means that 0.1 ends up
> serialized as "0.1" and not "0.09804" (Chrome) or "0.10196" (Presto and
> Safari)).

Actually, this bug was prompted by the fact that WebKit just changed to more Gecko-like behavior here:

http://trac.webkit.org/changeset/87512

The commit summary said "The number of decimals for alpha values between 0 and 1 is not specified, so match Gecko and Presto by returning the lowest number of decimals needed to represent the number", so my immediate reaction was to get the number of decimal places standardized.  (I don't know why it says "and Presto" -- I get the same results in Opera as you do.)
Comment 4 Boris Zbarsky 2011-05-31 15:58:36 UTC
> setting and then reading back strokeStyle or fillStyle will give you a
> different number of digits in different UAs for the alpha, depending on how
> much internal precision they have.

Yep.  And if you required the same number of digits, then you could still detect the limited internal precision.

If you're asking whether the fact that alpha is stored as an integer in the range 0-255 should be standardized, the answer is "maybe".  Right now the way floats are converted to such integers is not the same across UAs either, last I checked...  Standardizing makes some sense, but we need to be careful to not overconstrain future implementations that may want to allow a wider range of alpha values, assuming anyone would want that.  Worth a mailing list thread.
Comment 5 Michael[tm] Smith 2011-08-04 05:03:18 UTC
mass-move component to LC1
Comment 6 Ian 'Hixie' Hickson 2011-08-10 22:34:01 UTC
EDITOR'S RESPONSE: This is an Editor's Response to your comment. If you are satisfied with this response, please change the state of this bug to CLOSED. If you have additional information and would like the editor to reconsider, please reopen this bug. If you would like to escalate the issue to the full HTML Working Group, please add the TrackerRequest keyword to this bug, and suggest title and text for the tracker issue; or you may create a tracker issue yourself, if you are able to do so. For more details, see this document:
   http://dev.w3.org/html5/decision-policy/decision-policy.html

Status: Accepted
Change Description: see diff given below
Rationale: Concurred with comments above.

I tried. Not sure I like the result.
Comment 7 contributor 2011-08-10 22:34:12 UTC
Checked in as WHATWG revision r6411.
Check-in comment: Try to tighten up the definition of how to serialise rgba() in canvas.
http://html5.org/tools/web-apps-tracker?from=6410&to=6411