Personal tools

Library for colours

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
m
(add link to hackag)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
[[Category:Code]]
 
[[Category:Code]]
  +
  +
''See also the more sophisticated [http://hackage.haskell.org/package/colour colour package] on Hackage.''
   
 
Simple thing for working on colours in the RGB colours space. (The intention being that each component is in the interval 0 ≤ x ≤ 1.) You could just use tuples, but this library provides simple colour arithmetic.
 
Simple thing for working on colours in the RGB colours space. (The intention being that each component is in the interval 0 ≤ x ≤ 1.) You could just use tuples, but this library provides simple colour arithmetic.
Line 42: Line 44:
 
cclip :: Colour -> Colour
 
cclip :: Colour -> Colour
 
cclip = cmap clip
 
cclip = cmap clip
 
quantinize :: Int -> Double -> Int
 
quantinize scale v = floor (v * (fromIntegral scale))
 
 
</haskell>
 
</haskell>

Latest revision as of 22:40, 8 December 2010


See also the more sophisticated colour package on Hackage.

Simple thing for working on colours in the RGB colours space. (The intention being that each component is in the interval 0 ≤ x ≤ 1.) You could just use tuples, but this library provides simple colour arithmetic.

module Colour where
 
data Colour = Colour {red, green, blue :: Double} deriving (Eq, Show)
 
cmap :: (Double -> Double) -> Colour -> Colour
cmap f (Colour r g b) = Colour (f r) (f g) (f b)
 
czip :: (Double -> Double -> Double) -> Colour -> Colour -> Colour
czip f (Colour r1 g1 b1) (Colour r2 g2 b2) = Colour (f r1 r2) (f g1 g2) (f b1 b2)
 
cfold :: (Double -> Double -> Double) -> Colour -> Double
cfold f (Colour r g b) = r `f` g `f` b
 
cpromote :: Double -> Colour
cpromote x = Colour x x x
 
instance Num Colour where
  (+) = czip (+)
  (-) = czip (-)
  (*) = czip (*)
  negate = cmap negate
  abs    = cmap abs
  signum = cmap signum
  fromInteger x = cpromote (fromInteger x)
 
instance Fractional Colour where
  (/) = czip (/)
  recip = cmap recip
  fromRational x = cpromote (fromRational x)
 
clip :: (Num n, Ord n) => n -> n
clip n
  | n < 0 = 0
  | n > 1 = 1
  | otherwise = n
 
cclip :: Colour -> Colour
cclip = cmap clip