# Colour

### From HaskellWiki

m (formatting) |
(Getting Colour Components) |
||

Line 25: | Line 25: | ||

will create the colour with those [http://en.wikipedia.org/wiki/SRGB sRGB] colour coordinates. | will create the colour with those [http://en.wikipedia.org/wiki/SRGB sRGB] colour coordinates. | ||

− | If you have three <hask>Double</hask>s named <hask>red</hask>, <hask>green</hask>, and <hask>blue</hask>, then | + | If you have three <hask>Double</hask>s (or whatever you are using for your internal representation) named <hask>red</hask>, <hask>green</hask>, and <hask>blue</hask>, then |

<haskell> | <haskell> | ||

Line 65: | Line 65: | ||

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

+ | |||

+ | == Getting colour coordinates out == | ||

+ | |||

+ | To retrieve the [http://en.wikipedia.org/wiki/SRGB sRGB] coordinates of a colour, use the functions found in the <hask>Data.Colour.SRGB</hask> module. To get coordinates as <hask>Double<hask>s (or whatever your internal representation is) use <hask>toSRGB</hask>. For example | ||

+ | |||

+ | <haskell> | ||

+ | toSRGB chartreuse | ||

+ | </haskell> | ||

+ | |||

+ | will produce a value of type <hask>RGB Double</hask>. | ||

+ | |||

+ | === RGB triples === | ||

+ | |||

+ | The type <hask>RGB</hask> is special type of (strict) triple used to store colour coordinates. The functions <hask>channelRed</hask>, <hask>channelGreen</hask>, and <hask>channelBlue</hask> can be used to access the three fields. The constructor <hask>RGB</hask> will created a such a triple. For example, | ||

+ | |||

+ | <haskell> | ||

+ | RGB 0.5 0.4 0.6 | ||

+ | </haskell> | ||

+ | |||

+ | You might find the functions | ||

+ | |||

+ | <haskell> | ||

+ | curryRGB :: (RGB a -> b) -> a -> a -> a -> b | ||

+ | uncurryRGB :: (a -> a -> a -> b) -> RGB a -> b | ||

+ | </haskell> | ||

+ | |||

+ | useful when working with functions that operate on RGB triples. | ||

+ | |||

+ | === Back to colour coordinates === | ||

+ | |||

+ | Recall that | ||

+ | |||

+ | <haskell> | ||

+ | toSRGB chartreuse | ||

+ | </haskell> | ||

+ | |||

+ | produces an <hask>RGB Double<hask>. The coordinates output by <hask>toSRGB</hask> will all be between 0 and 1 unless the colour is out of gamut. | ||

+ | |||

+ | If you want to retrieve the colour coordinates as <hask>Word8</hask>s, use <hask>toSRGB24</hask> | ||

+ | |||

+ | <haskell> | ||

+ | toSRGB24 khaki | ||

+ | </haskell> | ||

+ | |||

+ | will produce an <hask>RGB Word8</hask>. Out of gamut channels be clamped to either to the range 0 to 255. | ||

+ | |||

+ | Lastly, the functions <hask>sRGB24show</hask> and <hask>sRGB24shows</hask> will produce colour strings of the form <hask>"#00aaff"</hask>. |

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

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

## Contents |

## 1 The Colour data type

The`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## 2 Creating colours

A collections of colours given by name can be found in thesRGB24 red green blue

will create the colour with those sRGB colour coordinates.

If you have threesRGB red green blue

## 3 Manipulating Colours

The colour operations are found in thethe 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 ofaffineCombo [(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 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## 4 Getting colour coordinates out

To retrieve the sRGB coordinates of a colour, use the functions found in thetoSRGB chartreuse

### 4.1 RGB triples

The typeRGB 0.5 0.4 0.6

You might find the functions

curryRGB :: (RGB a -> b) -> a -> a -> a -> b uncurryRGB :: (a -> a -> a -> b) -> RGB a -> b

useful when working with functions that operate on RGB triples.

### 4.2 Back to colour coordinates

Recall that

toSRGB chartreuse

toSRGB24 khaki