Colour
From HaskellWiki
(semi-transparent colours) |
m |
||
| Line 31: | Line 31: | ||
</haskell> | </haskell> | ||
| - | will produce the colour with those colour coordinates. These <hask>Double</hask> 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). | + | will produce the colour with those colour coordinates. These <hask>Double</hask> should be in the range [0,1] otherwise the resulting colour would be out of gamut (a [http://en.wikipedia.org/wiki/Gamut colour gamut] is a collection of representable colours on a device, such as your monitor). |
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>. | ||
| Line 64: | Line 64: | ||
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 | + | Lastly, colours are instance of a [[Monoid]] 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 == | == Getting colour coordinates out == | ||
| Line 157: | Line 157: | ||
will produce the semi-transparent colour resulting from acTop being composited over top of <hask>acBottom</hask>. The bottom layer, <hask>acBottom</hask> can be a non-transparent colour (of type <hask>Colour</hask>). In this case the result will also be a non-transparent colour. However, the top layer must be of semi-transparent type (although it could, of course, be opaque). | will produce the semi-transparent colour resulting from acTop being composited over top of <hask>acBottom</hask>. The bottom layer, <hask>acBottom</hask> can be a non-transparent colour (of type <hask>Colour</hask>). In this case the result will also be a non-transparent colour. However, the top layer must be of semi-transparent type (although it could, of course, be opaque). | ||
| - | Compositing is such important operation on semi-transparent colours, that it is the | + | Compositing is such important operation on semi-transparent colours, that it is the [[Monoid]] instance for <hask>AlphaColour a</hask>. The function <hask>mappend</hask> is <hask>over</hask>, and <hask>mempty</hask> is <hask>transparent</hask>. |
=== Getting semi-transparent coordinates === | === Getting semi-transparent coordinates === | ||
| Line 174: | Line 174: | ||
Note however, that transparent has no pure colour, and this case needs to be handled specially. | Note however, that transparent has no pure colour, and this case needs to be handled specially. | ||
| - | This | + | This operation is not natively provided because it is an operation that should be avoided. It is only really useful for interfacing with libraries that require pure colour components. Ideally it would be these libraries that implement conversion to and from <hask>Colour</hask>. However, you may find it necessary to implement the conversion functions yourself, in which case you can use the above "trick" to write the conversion function. |
Revision as of 05:32, 10 July 2009
This page provides a short introduction to using the colour package on hackage.
Contents |
1 The Colour data type
TheColour DoubleYou may wish to make a type synonym for this type in your program if you will use it everywhere.
You can always use the2 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 greenwill 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 usingdarken 0.4 turquoisewill 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 using4 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
5 Transparent Colour
Colours that are semi transparent are represented by theopaque goldenrod
moccasin `withOpacity` 0.7creates a colour that is 70% opaque and hence 30% transparent.
The valuedisolve 0.6 acanyColour `withOpacity` opacity == disolve opacity (opaque anyColour)
acTop `over` acBottom
5.1 Getting semi-transparent coordinates
The opacity of a semi-transparent colour can be retrieved by thepureColour ac | a > 0 = darken (recip a) (ac `over` (mempty::Colour Double)) | otherwise = error "transparent has no pure colour" where a = alphaChannel ac
Note however, that transparent has no pure colour, and this case needs to be handled specially.
This operation is not natively provided because it is an operation that should be avoided. It is only really useful for interfacing with libraries that require pure colour components. Ideally it would be these libraries that implement conversion to and from