# Colour

### From HaskellWiki

(Difference between revisions)

(Creating a short colour tutorial) |
(Manipulating colours) |
||

Line 34: | Line 34: | ||

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

+ | |||

+ | == Manipulating Colours == | ||

+ | |||

+ | The colour operations are found in the <hask>Data.Colour</hask> module. | ||

+ | The most common operation on colours is <hask>blend</hask>. For example, | ||

+ | the function | ||

+ | |||

+ | <haskell> | ||

+ | blend 0.25 red green | ||

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

+ | |||

+ | If you need to blend more than two colours, you can use multiple applications of <hask>blend</hask>, or you can use <hask>affineCombo</hask>. For example, | ||

+ | |||

+ | <haskell> | ||

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

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

+ | |||

+ | Color intensity can be changed by using <hask>darken</hask>. For example, | ||

+ | |||

+ | <haskell> | ||

+ | darken 0.4 turquoise | ||

+ | <haskell> | ||

+ | |||

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

## Revision as of 04:19, 10 July 2009

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

## 1 The Colour data type

TheColour a

Data.Colour

a

`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 thecolourConvert

## 2 Creating colours

A collections of colours given by name can be found in theData.Colour.Names

readColourName

tan

Data.Colour.SRGB

Word8

red

green

blue

sRGB24 red green blue

will create the colour with those sRGB colour coordinates.

If you have threeDouble

red

green

blue

sRGB red green blue

Double

sRGB24read

sRGB24reads

"#00aaff"

"00aaff"

## 3 Manipulating Colours

The colour operations are found in theData.Colour

blend

the function

blend 0.25 red green <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. If you need to blend more than two colours, you can use multiple applications of <hask>blend</hask>, or you can use <hask>affineCombo</hask>. For example, <haskell> affineCombo [(0.25,red),(0.5,green)] violet <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. Color intensity can be changed by using <hask>darken</hask>. For example, <haskell> darken 0.4 turquoise <haskell> 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 <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.