Copyright | (c) 2022 Mirko Westermeier |
---|---|
License | MIT |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
RGB color handling library
Synopsis
- data RGB a = RGB {}
- toList :: RGB a -> [a]
- fromList :: [a] -> Maybe (RGB a)
- showHex :: PrintfArg a => RGB a -> String
- parseRGB :: String -> Maybe (RGB Int)
- type Image = [[RGB Int]]
- type ImageList = [[[Int]]]
- toImageList :: Image -> ImageList
- fromImageList :: ImageList -> Maybe Image
- greyScale :: RGB Int -> RGB Int
- color2grey :: Image -> Image
- color2grey' :: ImageList -> Maybe ImageList
The RGB color type
A triple type representing the three color components of RGB color values.
Since most systems expect component values from 0 to 255, unsigned integer
types like
or RGB
Word
are most useful for color values.
But keeping it parameterized here allows for a useful RGB
Int
Applicative
instance
(see below).
Instances
Functor RGB # | Application of a given function to all color components: fmap (*2) (RGB 2 7 21) == RGB 4 14 42 |
Applicative RGB # | The (RGB (+5) (`div` 2) (*6) <*> RGB 12 74 7) == RGB 17 37 42 |
Eq a => Eq (RGB a) # | Default |
Show a => Show (RGB a) # | Default |
Utilty functions
Creates a three element list of RGB
color components:
toList (RGB 17 37 42) == [17, 37, 42]
fromList :: [a] -> Maybe (RGB a) #
Tries to create a color from a list of values, assuming exactly three values:
fromJust (fromList [17, 37, 42]) == RGB 17 37 42
Images
Super-simple image representation as a two-dimensional list of pixels
of type
.RGB
a
Super-oversimplified image representation as a three-dimensional list
of color components of type Int
.
fromImageList :: ImageList -> Maybe Image #
Codewars greyscale conversion
greyScale :: RGB Int -> RGB Int #
Simple greyscale conversion of a
color according to the
Codewars kata
Convert Color image to greyscale:RGB
Int
greyScale (RGB r g b) == RGB ((r+g+b)/3) ((r+g+b)/3) ((r+g+b)/3)
but using Fractional
arithmetics.