packages feed

prizm 0.1.0.1 → 0.1.0.2

raw patch · 12 files changed

+205/−47 lines, 12 filesdep +QuickCheckdep +prizmdep +test-frameworkdep ~base

Dependencies added: QuickCheck, prizm, test-framework, test-framework-quickcheck2

Dependency ranges changed: base

Files

+ CHANGES view
@@ -0,0 +1,23 @@+0.1.0.2+=======++- CIE XYZ to CIE L*a*b* formula implemented.+- CIE L*a*b* to CIE XYZ formula implemented.+- Implemented better fractional values in the formulas gleaned from+  this website: [CIE Conversion Mathematics](http://rip94550.wordpress.com/2011/07/04/color-cielab-and-tristimulus-xyz/).+- Comprehensive QuickCheck tests finished and pass with a lossless+  conversion at 11 decimal places.++0.1.0.1+=======++- SRGB to CIE XYZ formula implemented.+- CIE XYZ to SRGB formula implemented.+- Comprehensive QuickCheck tests finished and pass (conversion is lossless).++0.1.0.0+=======++- Package set up, types figured out.+- Research on different color representations, illuminants,+  chromaticity, etc...
README.md view
@@ -7,6 +7,11 @@ use in stylesheets and I wanted the same thing for use in [Clay](http://fvisser.nl/clay/) (also for [Bentonite](https://github.com/ixmatus/bentonite)). +## Colour Module++There is some overlap with the Haskell Colour Module that already exists; however, this library is filling a couple of+needs the other doesn't satisfy. I also want to have a cleaner API to work with.+ ## Supported Algorithms  - sRGB -> CIE XYZ@@ -14,10 +19,13 @@  ## Roadmap -### 1.0.0+### 0.1.0.2+Make sure all of the conversion formulas convert losslessly at an acceptable level of precision.++### 1.0.0.0 Implementation of conversions for all the CIE color representations and a converter for HEX to SRGB (trivial). -### 1.1.0+### 1.1.0.0 Implementations for  - color mixing@@ -26,5 +34,6 @@ - hue - saturation - inversion-+[General Color Formulas, Data, and Algorithms](http://www.brucelindbloom.com/index.html?Info.html)+[CIE Conversion Mathematics](http://rip94550.wordpress.com/2011/07/04/color-cielab-and-tristimulus-xyz/) [Conversion Algorithm Sources](http://www.easyrgb.com/index.php?X=MATH&H=01)
prizm.cabal view
@@ -1,11 +1,11 @@ Name:                prizm-Version:             0.1.0.1+Version:             0.1.0.2 Synopsis:            A haskell library for computing with colors  Description:         Prizm can convert between many different color                      representations; notably the sRGB and CIE                      representations. Future versions will-                     implementations for mutating colors as well.+                     have implementations for mutating colors as well.  Homepage:            https://github.com/ixmatus/prizm License:             BSD3@@ -24,6 +24,7 @@ extra-source-files:     LICENSE     README.md+    CHANGES  source-repository head   type:     git@@ -33,12 +34,30 @@   hs-source-dirs: src   default-language: Haskell2010   exposed-modules:-    Data.Prizm     Data.Prizm.Types     Data.Prizm.Color.Transform     Data.Prizm.Color.SRGB     Data.Prizm.Color.CIE+    Data.Prizm.Color.Matrices.RGB+    Data.Prizm.Color.Matrices.XYZ      ghc-options: -Wall      build-depends: base ==4.5.*++test-suite tests+  type:           exitcode-stdio-1.0+  hs-source-dirs: tests+  main-is:        QC.hs+  other-modules:  QC.SRGB+--                  QC.Text++  ghc-options:+    -Wall -threaded -rtsopts++  build-depends:+    prizm,+    base >= 4,+    QuickCheck >= 2.4,+    test-framework >= 0.4,+    test-framework-quickcheck2 >= 0.2
− src/Data/Prizm.hs
@@ -1,12 +0,0 @@-module Data.Prizm-(-  module Data.Prizm.Types-, module Data.Prizm.Color.Transform-, module Data.Prizm.Color.SRGB-, module Data.Prizm.Color.CIE-) where--import Data.Prizm.Types-import Data.Prizm.Color.Transform-import Data.Prizm.Color.CIE-import Data.Prizm.Color.SRGB
src/Data/Prizm/Color/CIE.hs view
@@ -1,33 +1,76 @@ module Data.Prizm.Color.CIE (   toRGB+, toRGBMatrix+, toLAB+, toXYZ ) where +import Control.Applicative+ import Data.Prizm.Types import Data.Prizm.Color.Transform+import Data.Prizm.Color.Matrices.XYZ -import Control.Applicative+-- 2deg observer, d65 illuminant+-- [x,y,z]+refWhite :: [Double]+refWhite = [95.047, 100.000, 108.883] -matrix :: [[Double]]-matrix = [-  [3.2406, (-1.5372), (-0.4986)],-  [(-0.9689), 1.8758, 0.0415],-  [0.0557, (-0.2040), 1.0570]]+-- | exact rational of the "0.008856" value.+v1 :: Double+v1 = (6/29) ** 3 --- | @transform@ transform an XYZ integer to be computed against+-- | exact rational of the "7.787" value.+v2 :: Double+v2 = 1/3 * ((29/6) ** 2)++-- | @transformRGB@ transform an XYZ integer to be computed against -- the xyzToRGB matrix.-transform :: Double -> Integer-transform v | v > 0.0031308 = min (truncate ((1.055 * (v ** (1 / 2.4)) - 0.055) * 255)) 255-            | otherwise     = min (truncate ((12.92 * v) * 255)) 255+transformRGB :: Double -> Integer+transformRGB v | v > 0.0031308 = min (round ((1.055 * (v ** (1 / 2.4)) - 0.055) * 255)) 255+               | otherwise     = min (round ((12.92 * v) * 255)) 255 +transformLAB :: Double -> Double+transformLAB v | v > v1 = v ** (1/3)+               | otherwise    = (v2 * v) + (16 / 116)++transformXYZ :: Double -> Double+transformXYZ v | cv > v1 = cv+               | otherwise     = (v - 16 / 116) / v2+    where cv = v**3+ -- | @toRGB@ convert a CIE color to an SRGB color. --  -- Once I've implemented CIE L*a*b -> XYZ and vice-versa functions -- then I'll introduce the type exhaustively here to handle any CIE -- color -> SRGB conversion.-toRGB :: CIE -> SRGB---toRGB (LAB _ _ _) = Nothing-toRGB (XYZ x y z) =+toRGB :: CIE -> RGB+toRGB = (toRGBMatrix d65SRGB)++toRGBMatrix :: XYZtoRGB -> CIE -> RGB+toRGBMatrix m (LAB l a b) = toRGBMatrix m (toXYZ (LAB l a b))+toRGBMatrix m (XYZ x y z) =     let t = ZipList ((/100) <$> [x,y,z])-        [r,g,b] = (transform) <$> ((zipTransform t) <$> matrix)-    in SRGB r g b+        [r,g,b] = (transformRGB) <$> ((zipTransform t) <$> m)+    in RGB r g b++toLAB :: CIE -> CIE+toLAB (LAB l a b) = LAB l a b+toLAB (XYZ x y z) =+    let v = getZipList $ ZipList ((/) <$> [x,y,z]) <*> ZipList refWhite+        [tx,ty,tz] = (transformLAB) <$> v+        l = (116 * ty) - 16+        a = 500 * (tx - ty)+        b = 200 * (ty - tz)+    in LAB l a b++toXYZ :: CIE -> CIE+toXYZ (XYZ x y z) = XYZ x y z+toXYZ (LAB l a b) =+    let y = (l + 16) / 116+        x = a / 500 + y+        z = y - b / 200+        [nx,ny,nz] = getZipList $ ((*) <$> ZipList ((transformXYZ) <$> [x,y,z])) <*> ZipList refWhite+    in XYZ nx ny nz+    
+ src/Data/Prizm/Color/Matrices/RGB.hs view
@@ -0,0 +1,17 @@+module Data.Prizm.Color.Matrices.RGB where++import Data.Prizm.Types (RGBtoXYZ)++-- http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html++d65SRGB :: RGBtoXYZ+d65SRGB =  [+  [0.4124564, 0.3575761, 0.1804375],+  [0.2126729, 0.7151522, 0.0721750],+  [0.0193339, 0.1191920, 0.9503041]]++d65Adobe :: RGBtoXYZ+d65Adobe =  [+  [0.5767309],[0.1855540],[0.1881852],+  [0.2973769],[0.6273491],[0.0752741],+  [0.0270343],[0.0706872],[0.9911085]]
+ src/Data/Prizm/Color/Matrices/XYZ.hs view
@@ -0,0 +1,17 @@+module Data.Prizm.Color.Matrices.XYZ where++import Data.Prizm.Types (XYZtoRGB)++-- http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html++d65SRGB :: XYZtoRGB+d65SRGB =  [+  [3.2404542, (-1.5371385), (-0.4985314)],+  [(-0.9692660), 1.8760108, 0.0415560],+  [0.0556434, (-0.2040259), 1.0572252]]++d65Adobe :: XYZtoRGB+d65Adobe =  [+  [2.0413690],[(-0.5649464)],[(-0.3446944)],+  [(-0.9692660)],[1.8760108],[0.0415560],+  [0.0134474],[(-0.1183897)],[1.0154096]]
src/Data/Prizm/Color/SRGB.hs view
@@ -1,30 +1,29 @@ module Data.Prizm.Color.SRGB (   toXYZ+, toXYZMatrix ) where  import Data.Prizm.Types import Data.Prizm.Color.Transform+import Data.Prizm.Color.Matrices.RGB  import Control.Applicative -matrix :: [[Double]]-matrix = [-  [0.4124, 0.3576, 0.1805],-  [0.2126, 0.7152, 0.0722],-  [0.0193, 0.1192, 0.9505]]- -- | @rgbTransform@ transform an RGB integer to be computed against--- the rgbToXYZ matrix.+-- a matrix. transform :: Integer -> Double transform v | dv > 0.04045 = (((dv + 0.055) / ap) ** 2.4) * 100-               | otherwise    = (dv / 12.92) * 100+            | otherwise    = (dv / 12.92) * 100     where dv = fromIntegral v / 255           ap = 1.0 + 0.055  -- | @toXYZ@ convert an sRGB value to a CIE XYZ value.-toXYZ :: SRGB -> CIE-toXYZ (SRGB r g b) =+toXYZ :: RGB -> CIE+toXYZ = (toXYZMatrix d65SRGB)++toXYZMatrix :: RGBtoXYZ -> RGB -> CIE+toXYZMatrix m (RGB r g b) =     let t = ZipList (transform <$> [r,g,b])-        [x,y,z] = (zipTransform t) <$> matrix+        [x,y,z] = (roundN 3) <$> ((zipTransform t) <$> m)     in XYZ x y z
src/Data/Prizm/Color/Transform.hs view
@@ -4,5 +4,11 @@  import Control.Applicative +roundN :: Integer -> Double -> Double+roundN n num = (fromInteger $ round $ num * (10^n)) / (10.0^^n)++truncateN :: Integer -> Double -> Double+truncateN n num = (fromInteger $ truncate $ num * (10^n)) / (10.0^^n)+ zipTransform :: ZipList Double -> [Double] -> Double zipTransform tv m = sum $ getZipList $ (*) <$> ZipList m <*> tv
src/Data/Prizm/Types.hs view
@@ -1,10 +1,13 @@ module Data.Prizm.Types where -data SRGB-    = SRGB Integer Integer Integer+type RGBtoXYZ = [[Double]]+type XYZtoRGB = [[Double]]++data RGB+    = RGB Integer Integer Integer     deriving (Eq, Ord, Show)  data CIE     = XYZ Double Double Double---    | LAB Double Double Double+    | LAB Double Double Double     deriving (Eq, Ord, Show)
+ tests/QC.hs view
@@ -0,0 +1,15 @@+{-# OPTIONS_GHC -fno-warn-missing-signatures #-}++module Main (main) where++import qualified QC.SRGB as SRGB+import qualified QC.CIE as CIE++import Test.Framework (defaultMain, testGroup)++main = defaultMain tests++tests = [+      testGroup "srgb" SRGB.tests+    , testGroup "cie" CIE.tests+      ]
+ tests/QC/SRGB.hs view
@@ -0,0 +1,19 @@+module QC.SRGB (tests) where++import Test.Framework.Providers.QuickCheck2 (testProperty)+import Test.QuickCheck++import Control.Monad++import Data.Prizm.Color.SRGB as S+import Data.Prizm.Color.CIE as C+import Data.Prizm.Types++instance Arbitrary RGB where+    arbitrary = liftM3 RGB (choose (0, 255)) (choose (0, 255)) (choose (0, 255))++rgb2XYZ v = C.toRGB(S.toXYZ v) == v++tests = [+    testProperty "SRGB <-> CIE XYZ" rgb2XYZ+      ]