About canvas color definitions

  Read comments...

Summary
Information about the different ways of specifying colors with HTML5 canvas. A lot of the ways to specify color will be familiar though there are a few methods that will be new, as well as a few new CSS3 methods.

You will probably want to know the different ways you can define a color. There are a few ways which you can use, all of which are quite straight-forward.

 

Named colors

The first, and easiest, is to use named colors. This gives you a range of color values that have been predefined for you. Eg:

obj.set({
    colors: ['red', 'blue']
});

 

Hex values

The next, is straight forward hex values like you can use in normal CSS. These consist of a hash sign, followed by three or six hexadecimal characters. Eg:

obj.set({
    colors: ['#f00', '#0000ff']
});

 

RGB values

Also as used in CSS, RGB values are the same as what you can use in CSS. Eg:

obj.set({
    colors: ['rgb(255,0,0)', 'rgb(0,0,255)']
});

 

RGBA values

Probably new to most people, is rgba(). Similar to regular rgb() values, but with a fourth value that allows you to specify the alpha value, which stipulates how transparent the color is. An alpha value of 0 is totally transparent, and a value of 1, is totally opaque (ie. you can't see through it). With a value of 1, rgba() acts exactly like rgb(). This example gives you red and blue colors that are semi-transparent:

obj.set({
    colors: ['rgba(255,0,0,0.5)', 'rgba(0,0,255,0.5)']
});

 

HSL values

Also probably quite new to you, are hsl() values. Much like rgb(), but instead of red green and blue, it allows you to specify the Hue, Saturation and Light values instead. For example:

obj.set({
    colors: ['hsl(255, 100%, 50%)', 'hsl(169, 100%, 50%)']
});

 

HSLA values

Much like rgb() and rgba(), hsl() has a corresponding hsla() version, which allows you to specify an alpha (transparency) value. Eg:

obj.set({
    colors: ['hsla(255, 100%, 50%, 0.5)', 'hsla(169, 100%, 50%, 0.5)']
});

 

Linear gradients

Gradients can be used in place of color values. You can create a linear gradient like so:

var grad = obj.context.createLinearGradient(0,0,0,250);
grad.addColorStop(0, 'red');
grad.addColorStop(1, 'blue');

This creates a gradient that goes from red to blue. The gradient starts at 0,0, and finishes at 0,250. ie A vertical gradient. You may not see the whole gradient - that depends on the extent of the shape you're filling. You can use the gradient in place of a regular color definition. Eg:

obj.set({
    colors: [myGradient]
});

There is a shortcut function that is part of the RGraph API that can make creating gradients easier:

var grad = RGraph.linearGradient(obj, x1, y1, x2, y2, color1, color2);

And you can of course use the gradient shorthand described below.

 

Radial gradients

Radial gradients are much like their linear counterparts, but circular, as the name suggests. Eg:

var grad = obj.context.createRadialGradient(0,0,0,0,0,100);
grad.addColorStop(0, 'red');
grad.addColorStop(1, 'blue');

Instead of four arguments, it takes six - the coordinates of the start point along with the radius, and the coordinates of the end point, along with the radius. After it has been created, you can treat as you would a linear gradient:

obj.set({
    colors: [myGradient]
});

There is a shortcut function that is part of the RGraph API that can make creating gradients easier:

var grad = RGraph.radialGradient(obj, x1, y1, r1, x2, y2, r2, color1, color2);

And you can of course use the gradient shorthand described below.

 

RGraph gradient shorthand

The RGraph custom shorthand for gradients is new in October 2012 and makes using gradients much easier and less verbose. Instead of a color you can specify this: Gradient(red:white). You can specify 2 or more colors and where it makes sense RGraph will use a radial gradient over a linear gradient. There's an example of it in use on the Rose chart example page.

<script>
    window.onload = (function ()
    {
        var rose = new RGraph.Rose({
            id: 'cvs',
            data: [8,8,9,6,8,7,7],
            options: {
                colors: ['Gradient(red:white)'],
                labelsAxes: 'n',
                margin: 3
            }
        }).draw()
    })
</script>

 

More information

You can read more about CSS3 color definitions here.

Share RGraph
X

Comments