Bookmark and share:
Bookmark with delicious tweet this site Post to Google Buzz! Post to Facebook Share on FriendFeed Stumble! this site

RGraph: HTML5 canvas graph library - About canvas color definitions

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:

myObject.Set('chart.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:

myObject.Set('chart.colors', ['#f00', '#0000ff']);


RGB values

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

myObject.Set('chart.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:

myObject.Set('chart.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:

myObject.Set('chart.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:

myObject.Set('chart.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:

myGradient = myObject.context.createLinearGradient(0,0,0,250);
myGradient.addColorStop(0, 'red');
myGradient.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:

myObject.Set('chart.colors', [myGradient]);


Radial gradients

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

myGradient = myObject.context.createRadialGradient(0,0,0,0,0,100);
myGradient.addColorStop(0, 'red');
myGradient.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:

myObject.Set('chart.colors', [myGradient]);


More information

You can read more about CSS3 color definitions here.