[whatwg] Canvas color serialisation

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.


> 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.

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.)


On Sat, 12 May 2007, Philip Taylor wrote:
> 
> Being able to set colours by a [r,g,b(,a)] array would be slightly
> useful for anyone computing colours. I've done some code which deals
> with colours as arrays internally, with a function
>    function array_to_rgba(c) {
>        return 'rgba('+c[0]+','+c[1]+','+c[2]+',1)';
>    }
> to get an output format the canvas understands. (If I wanted to be
> slightly fancier I'd probably have to do
>    function array_to_rgba(c) {
>        return 'rgba(' + Math.floor(c[0]) + ',' + Math.floor(c[1]) +
> ',' + Math.floor(c[2]) + ',' + (c.length==4 ? c[3]/255 : 1) + ')';
>    }
> instead.)
> 
> As other examples, there's PlotKit at
> http://projects.liquidx.net/js/browser/plotkit/trunk/PlotKit using
> MochiKit's Color.toRGBString, and people constructing the strings
> inline with:
>    "rgba("+((c1>0)?c1:0)+","+((c2>0)?c2:0)+","+((c3>0)?c3:0)+","+((max_iteration-c)/max_iteration)+")";
> at http://virtuelvis.com/download/2005/10/mandelbrot/
>    'rgba('+(face[i/4]%2==0 ? shade1 : 0)+','+(face[i/4]==1 ? shade1 :
> 0)+','+(face[i/4]==2 ? 0 : shade1)+',1)' at
> http://www.abrahamjoffe.com.au/ben/canvascape/
>    "rgb("+rand_red+","+rand_green+","+rand_blue+")" at
> http://dave-webster.com/projects/index.php?page=incs/plasma_demo1
> 
> so people do want to set colours from calculated values, and the 
> CSS-colour interface just becomes an inconvenience. But it's only a 
> small inconvenience and is easy to work around, so it may not matter 
> enough to make changes.

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.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Tuesday, 15 May 2007 16:11:19 UTC