Code Monkey home page Code Monkey logo

color's Introduction

Color

A library for dealing with Colors and pixels. It implements arbitrary color space conversion, chromatic adaptation and other color manipulations.

Status

Language Github Actions Coveralls Gitter.im
GitHub top language Build Status Coverage Status Join the chat at https://gitter.im/haskell-massiv/Lobby
Package Hackage Nightly LTS
Color Hackage Nightly Nightly

Description

There is a clear separation between color models, color spaces and alternative representations of color spaces. All are distinct at the type level. The goal is to prevent mixups of incompatible color types as well as utilize type information for conversion between them.

Currently supported:

  • Color models:

    • Y
    • RGB
    • HSI
    • HSL
    • HSV
    • YCbCr
    • CMYK
  • Color spaces and arbitrary conversions between them:

    • Y - luminance

    • Y' - luma

    • CIE XYZ

    • CIE L*a*b*

    • RGB:

      • sRGB - both standardized and derived

      • AdobeRGB - both standardized and derived

      • ITU: Rec470, Rec601 and Rec709

      • Alternative representations:

        • HSI
        • HSL
        • HSV
        • YCbCr
        • CMYK
  • Illuminants:

    • CIE1931 - 2 degree observer
    • CIE1964 - 10 degree observer
    • Some common alternatives
  • Chromatic adaptation:

    • VonKries adaptation with transformations:

      • VonKries
      • Bradford (default)
      • Fairchild
      • CIECAM02
      • CMCCAT2000
  • Color Standards:

    • 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:

$ stack ghci --package Color

Perceived lightness

Let's say we need find the perceived lightness as described in this StackOverflow answer for 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 the Illuminant and the Linearity of the RGB color space.
  2. Convert your RGB color to CIE L*a*b* 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, so we'll use that for this example:

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 sRGB to CIE L*a*b* color space we need to increase the precision to Double, because for now Word8 is not supported by the LAB color space implementation:

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*:

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

Color adaptation

When a change of Illuminant is also needed during color space conversion we can use convert function

ghci> import Graphics.Color.Adaptation (convert)
ghci> import qualified Graphics.Color.Illuminant.CIE1964 as CIE1964
ghci> let lab@(ColorLAB l _ _) = convert rgb :: Color (LAB 'CIE1964.D50) Double
ghci> lab
<LAB CIE1964 'D50:(90.2287735564601500,-59.3846969983265500,72.9304679742930800)>

External resources

color's People

Contributors

ashleyyakeley avatar gilgamec avatar kelsolaar avatar lehins avatar o1lo01ol1o avatar traviscardwell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

color's Issues

Add CIELUV, LCh(ab), LCh(uv) colorspaces

This is a request for adding the CIELUV colorspace, as well as the cylindrical views of LAB and LUV, LCh(ab) and LCh(uv).

I'd do this myself and make a PR, but I see that the modules for (most of) these are there, commented out, in the cabal file, and didn't want to duplicate work or clumsily trample tricky problems you had with implementation.

Add color difference algorithms

@ekmett mentioned on reddit that he'd like some color distance computation added to Color:

One thing I'd really like to have would be DIN99o color distance computations

It seems like a pretty good idea, I wanna record it before I forget about it. There are sample implementations available in:

so it shouldn't be too hard to port it into Haskell.

Fix Arbitrary instances for color spaces / models with unusual coordinate ranges

I noticed that the test specifications for e.g. LAB use the same Abritrary instance as others:

instance (Elevator e, Random e, Illuminant i) => Arbitrary (Color (LAB (i :: k)) e) where
  arbitrary = ColorLAB <$> arbitraryElevator <*> arbitraryElevator <*> arbitraryElevator

where arbitraryElevator gives either the full range of values (for discrete quanta like Word8) or the range [0,1] for Float and Double. However, LAB at least (and possibly others) uses a different range: L can go from 0 to 100, while a and b vary in a similar range, depending on L, and can also be negative. Thus, right now the spec tests are only checking colors very close to black. I'm not sure how this can be handled within QuickCheck.

Add functions for blending and overlaying

I'm not sure if there's already an easy way to do this, but it would be nice to have some of the same functionality that colour has. Something like:

  • blend :: (...) => e -> Color cs e -> Color cs e -> Color cs e
  • over :: (...) => Color (Alpha cs) e -> Color cs e -> Color cs e
  • overAlpha :: (...) => Color (Alpha cs) e -> Color (Alpha cs) e -> Color (Alpha cs) e

As I understand it, in colour, blends are always done in linear space.

add example for finding perceived lightness

Hello, I am trying to find out if this library can already do the computation to find perceived lightness as described in this post. If I have a (Word8, Word8, Word8) RGB triple, how can I determine perceived lightness using this library? I assume this is possible, but can someone help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.