RGB-0.1.0.0: RGB color handling library
Copyright(c) 2022 Mirko Westermeier
LicenseMIT
Safe HaskellSafe-Inferred
LanguageHaskell2010

RGB

Description

RGB color handling library

Synopsis

The RGB color type

data RGB a #

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 RGB Word or RGB Int are most useful for color values. But keeping it parameterized here allows for a useful Applicative instance (see below).

Constructors

RGB 

Fields

  • red :: a

    Red color value

  • green :: a

    Green color value

  • blue :: a

    Blue color value

Instances

Instances details
Functor RGB #

Application of a given function to all color components:

fmap (*2) (RGB 2 7 21)  ==  RGB 4 14 42
Instance details

Defined in RGB

Methods

fmap :: (a -> b) -> RGB a -> RGB b #

(<$) :: a -> RGB b -> RGB a #

Applicative RGB #

The Applicative instance of RGB can act as a component-wise application:

(RGB (+5) (`div` 2) (*6)  <*>  RGB 12 74 7)  ==  RGB 17 37 42
Instance details

Defined in RGB

Methods

pure :: a -> RGB a #

(<*>) :: RGB (a -> b) -> RGB a -> RGB b #

liftA2 :: (a -> b -> c) -> RGB a -> RGB b -> RGB c #

(*>) :: RGB a -> RGB b -> RGB b #

(<*) :: RGB a -> RGB b -> RGB a #

Eq a => Eq (RGB a) #

Default Eq instance. Two RGB values are equal iff all components red, green, blue are equal.

Instance details

Defined in RGB

Methods

(==) :: RGB a -> RGB a -> Bool #

(/=) :: RGB a -> RGB a -> Bool #

Show a => Show (RGB a) #

Default Show instance. Example: "RGB {red = 17, green = 37, blue = 42}". See showHex for a compressed hexadecimal stringification.

Instance details

Defined in RGB

Methods

showsPrec :: Int -> RGB a -> ShowS #

show :: RGB a -> String #

showList :: [RGB a] -> ShowS #

Utilty functions

toList :: RGB a -> [a] #

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

showHex :: PrintfArg a => RGB a -> String #

Hexadecimal color representation for printfable components. For values between 0 and 255 it is the inverse of parseRGB (modulo Maybe):

showHex (fromJust $ parseRGB $ "#11252a")  ==  "#11252a"

parseRGB :: String -> Maybe (RGB Int) #

Parse RGB Int values from hexadecimal strings like #aabbcc.

fromJust (parseRGB $ showHex $ RGB 17 37 42)  ==  RGB 17 37 42

Images

type Image = [[RGB Int]] #

Super-simple image representation as a two-dimensional list of pixels of type RGB a.

type ImageList = [[[Int]]] #

Super-oversimplified image representation as a three-dimensional list of color components of type Int.

toImageList :: Image -> ImageList #

Creates an ImageList from an Image.

fromImageList :: ImageList -> Maybe Image #

Tries to create an Image from an ImageList using fromList.

Codewars greyscale conversion

greyScale :: RGB Int -> RGB Int #

Simple greyscale conversion of a RGB Int color according to the Codewars kata Convert Color image to greyscale:

greyScale (RGB r g b)  ==  RGB ((r+g+b)/3) ((r+g+b)/3) ((r+g+b)/3)

but using Fractional arithmetics.

Greyscale images

color2grey :: Image -> Image #

Application of greyScale to each RGB Int pixel of an Image

color2grey' :: ImageList -> Maybe ImageList #

Application of greyScale to each RGB Int pixel of an ImageList