Skip to content

Commit

Permalink
Add an example to the README file
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Nov 28, 2022
1 parent 60fe65f commit c00ed47
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
54 changes: 54 additions & 0 deletions Color/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,60 @@ Currently supported:
* RAL
* SVG

## Example

Here is a short example how this library can be used. Here we assume a GHCi session that
can be started like so:

```shell
$ stack ghci --package Color
```

### Perceived lightness

Let's say we need find the perceived lightness as described in [this StackOverflow
answer](https://stackoverflow.com/questions/596216/formula-to-determine-perceived-brightness-of-rgb-color/56678483#56678483)
for a an RGB triple `(128, 255, 65) :: (Word8, Word8, Word8)`.

Before we can attempt getting the lightness we need to do these two things:

1. Figure out what is the color space of the `RGB` triplet? In particular what is the
`Illuminant` and what is the `Linearity` of the `RGB` color space is.
2. Convert your `RGB` color to `CIELAB` and then we can get the `L*` out, which is the
perceived lightness.


More often than not an RGB image will be encoded in non-linear sRGB color space with 8 bits
per channel:

```haskell
ghci> :set -XDataKinds
ghci> import Graphics.Color.Space
ghci> let rgb8 = ColorSRGB 128 255 65 :: Color (SRGB 'NonLinear) Word8
ghci> print rgb8
<SRGB 'NonLinear:(128,255, 65)>
```

Before we convert it to CIE LAB color space we need to increase the precesion to `Double`,
becasue for now `Word8` is not supported by CIELAB:

```haskell
ghci> let rgb = toDouble <$> rgb8
ghci> print rgb
<SRGB 'NonLinear:( 0.5019607843137255, 1.0000000000000000, 0.2549019607843137)>
```

In order to convert to another color space without changing the illuminant we can use
`convertColor` function. So here is how we convert to CIELAB and extract the perceived
lightness `L*`:

```haskell
ghci> let lab@(ColorLAB l _ _) = convertColor rgb :: Color (LAB D65) Double
ghci> lab
<LAB * D65:(90.0867507593648500,-65.7999116680496000,74.4643898323530600)>
ghci> l
90.08675075936485
```

## External resources

Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-17.14
resolver: lts-18.26
packages:
- Color
extra-deps: []

0 comments on commit c00ed47

Please sign in to comment.