[whatwg] Canvas color serialisation

Jordan OSETE jordan.osete at laposte.net
Mon May 21 13:53:16 PDT 2007

Ian Hickson wrote :
> On Sat, 12 May 2007, Jordan OSETE wrote:
>> It can be done with a wrapper, but it seems overhead, when the UA can 
>> just return something easier to read.
> Well, the overhead is the same, it's just a matter of who does it, the UA 
> or the author.

Though we would need confirmation from implementors, i thought UAs would 
store color internally as arrays-like stuff, for efficiency, and only 
convert those to or from CSS values when needed.

>> In that case, why not always return an array, like Philip Taylor 
>> suggested? It would allow the user be able to read color values in an 
>> easy way, and still keep compatibility with this kind of code :
>>>    var old = context.fillStyle;
>>>    context.fillStyle = 'green';
>>>    context.fillRect(0,0,100,100);
>>>    context.fillStyle = old;
>> I don't see many reasons to return strings like #xxxxxx or rgba(...) in 
>> the first place, but if needed, it's way easier for the application to 
>> convert that array[4] to a rgba(...) or #xxxxxx string than the other 
>> way around.
> One reason to get back CSS values is that it makes it trivial to poke 
> values into CSS sheets.

If CSS values are needed, it can still be converted quite easily:
    var col = context.fillStyle;              //get an array
    col.pop();                                //no alpha
    var css_col = "rgb(" + col.join(",") + ")";

While parsing the current return values is way harder than that.

> But the real reason is that the attribute takes CSS in, so it returning 
> CSS colours is symmetric and unsurprising. (Surprises are bad in APIs.)

I can understand that, but if it can take an array in, returning an 
array is also symetric.
BTW, behaving like an array would be more consistent with the way 
getImageData() and putImageData() work, returning an array of 4*w*h 
integer values between 0 and 255 (in that case, alpha could also be 
returned an integer).

>> [...]
> Agreed. I think we should see what people need once <canvas> has been more 
> widely used, and then update the API that way. It may be that we can have 
> the "string" actually be whatever the CSSOM ends up using to represent 
> colours, so that we can then do things like:
>    context.fillStyle.red += 1;
> ...and so on.

This is also a possibility.

PS: Sorry for the late answer.


Jordan Osete

More information about the whatwg mailing list