Difference between revisions of "Colour"

From HaskellWiki
Jump to: navigation, search
(Manipulating colours)
m (formatting)
Line 43: Line 43:
 
<haskell>
 
<haskell>
 
blend 0.25 red green
 
blend 0.25 red green
<haskell>
+
</haskell>
   
 
will create a new colour that is 25% red, and 75% green. The weight parameter (the first parameter) should be between 0 and 1, otherwise an out of gamut colour could result.
 
will create a new colour that is 25% red, and 75% green. The weight parameter (the first parameter) should be between 0 and 1, otherwise an out of gamut colour could result.
Line 51: Line 51:
 
<haskell>
 
<haskell>
 
affineCombo [(0.25,red),(0.5,green)] violet
 
affineCombo [(0.25,red),(0.5,green)] violet
<haskell>
+
</haskell>
   
 
will create a new colour that is 25% red, 50% green, and 25% violet. Again the weights should all be non-negative and the sum of the weights should be no more than 1, otherwise an out of gamut colour could result.
 
will create a new colour that is 25% red, 50% green, and 25% violet. Again the weights should all be non-negative and the sum of the weights should be no more than 1, otherwise an out of gamut colour could result.
Line 59: Line 59:
 
<haskell>
 
<haskell>
 
darken 0.4 turquoise
 
darken 0.4 turquoise
<haskell>
+
</haskell>
   
 
will produce a turquoise that is only 40% of the intensity of normal turquoise.
 
will produce a turquoise that is only 40% of the intensity of normal turquoise.
 
The weight parameter (the first parameter) should be between 0 and 1, otherwise an out of gamut colour could result. However if you know that the intensity is low enough, you may safe "darken" by values greater than 1 (which will actually lighten the colour).
 
The weight parameter (the first parameter) should be between 0 and 1, otherwise an out of gamut colour could result. However if you know that the intensity is low enough, you may safe "darken" by values greater than 1 (which will actually lighten the colour).
   
Lastly, colours are instance of a <hask>Monoid</hask> so colours can be "added" by using <hask>mappend</hask> (and <hask>mempty</hask> is a quick way to get black). However, like spotlights, adding colours makes more intense colours. Adding colours could take you out of gamut. Unless you specifically know you want to be adding colours, you probably want to be using <hask>blend</hask> instead.</haskell>
+
Lastly, colours are instance of a <hask>Monoid</hask> so colours can be "added" by using <hask>mappend</hask> (and <hask>mempty</hask> is a quick way to get black). However, like spotlights, adding colours makes more intense colours. Adding colours could take you out of gamut. Unless you specifically know you want to be adding colours, you probably want to be using <hask>blend</hask> instead.

Revision as of 04:20, 10 July 2009

This page provides a short introduction to using the colour package on hackage.

The Colour data type

The Colour a data type and its basic operations are found in the Data.Colour module. The type variable a is used to specify the numeric type used for the internal representation of the data. Typically one will use:

Colour Double

You may wish to make a type synonym for this type in your program if you will use it everywhere.

You can always use the colourConvert to change to a different internal representation type.

Creating colours

A collections of colours given by name can be found in the Data.Colour.Names module. There is also a readColourName to convert a string with one of these names into a colour. Be aware that the colour tan will conflict with the Prelude function unless you hide the Prelude function or import the module qualified.

Another way to make a colour is by specifying an RGB triple. These functions can be found in the Data.Colour.SRGB library. For example, if you have three Word8s named red, green, and blue, then

sRGB24 red green blue

will create the colour with those sRGB colour coordinates.

If you have three Doubles named red, green, and blue, then

sRGB red green blue

will produce the colour with those colour coordinates. These Double should be in the range [0,1] otherwise the resulting colour would be out of gamut (a colour gamut is a collection of representable colours on a device, such as your monitor).

Lastly, sRGB24read and sRGB24reads can create colour from string specifications of the form "#00aaff" or "00aaff".

Manipulating Colours

The colour operations are found in the Data.Colour module. The most common operation on colours is blend. For example, the function

blend 0.25 red green

will create a new colour that is 25% red, and 75% green. The weight parameter (the first parameter) should be between 0 and 1, otherwise an out of gamut colour could result.

If you need to blend more than two colours, you can use multiple applications of blend, or you can use affineCombo. For example,

affineCombo [(0.25,red),(0.5,green)] violet

will create a new colour that is 25% red, 50% green, and 25% violet. Again the weights should all be non-negative and the sum of the weights should be no more than 1, otherwise an out of gamut colour could result.

Color intensity can be changed by using darken. For example,

darken 0.4 turquoise

will produce a turquoise that is only 40% of the intensity of normal turquoise. The weight parameter (the first parameter) should be between 0 and 1, otherwise an out of gamut colour could result. However if you know that the intensity is low enough, you may safe "darken" by values greater than 1 (which will actually lighten the colour).

Lastly, colours are instance of a Monoid so colours can be "added" by using mappend (and mempty is a quick way to get black). However, like spotlights, adding colours makes more intense colours. Adding colours could take you out of gamut. Unless you specifically know you want to be adding colours, you probably want to be using blend instead.