[whatwg] Canvas color serialisation

On 12/05/07, Anne van Kesteren <annevk at opera.com> wrote:
> On Sat, 12 May 2007 10:52:39 +0200, Jordan OSETE
> <jordan.osete at laposte.net> wrote:
> > It can be done with a wrapper, but it seems overhead, when the UA can
> > just return something easier to read.
> > In that case, why not always return an array, like Philip Taylor
> > suggested?
>
> This would complicate setting fillStyle and strokeStyle. It's also not
> clear what the use case is, as I understand it.

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.

If arrays were accepted as input, then using arrays as return values
would seem to be a logical change - it would still allow people to do
'ctx.fillStyle = ctx.fillStyle' as before, and would make it easier if
anybody did want to parse the colours (though I don't know why they
would), and would probably simplify the spec a little (since the
paragraph about colour return values is a bit long and painful now),
and would gain consistency with the output format of getImageData.

-- 
Philip Taylor
excors at gmail.com

Received on Saturday, 12 May 2007 04:16:19 UTC