packages feed

geomancy 0.4.0.0 → 0.4.1.0

raw patch · API changes only · 8 files changed · hide whitespace changes · browse geomancy-0.4.1.0

+566/−1 lines, 8 filesPVP ok

API changes (from Hackage documentation)

0 removed, 21 added, 0 changed

version bump matches the API change (PVP)

+ Geomancy.Color: fromUnorm8 :: Word8 -> Float
+ Geomancy.Color: premultiply :: Vec4 -> Vec4
+ Geomancy.Color: toUnorm8 :: Float -> Word8
+ Geomancy.Color.Hue: fromHSL :: Vec3 -> Vec3
+ Geomancy.Color.Hue: fromHSV :: Vec3 -> Vec3
+ Geomancy.Color.Hue: hue :: Float -> Vec3
+ Geomancy.Color.Hue: toHSL :: Vec3 -> Vec3
+ Geomancy.Color.Hue: toHSV :: Vec3 -> Vec3
+ Geomancy.Color.OKLab: fromLCh :: Vec3 -> Vec3
+ Geomancy.Color.OKLab: fromLinear :: Vec3 -> Vec3
+ Geomancy.Color.OKLab: toLCh :: Vec3 -> Vec3
+ Geomancy.Color.OKLab: toLinear :: Vec3 -> Vec3
+ Geomancy.Color.SRGB: fromLinear :: Float -> Float
+ Geomancy.Color.SRGB: fromLinear3 :: Vec3 -> Vec3
+ Geomancy.Color.SRGB: fromLinear4 :: Vec4 -> Vec4
+ Geomancy.Color.SRGB: linearToXYZ :: Vec3 -> Vec3
+ Geomancy.Color.SRGB: luminance :: Vec3 -> Float
+ Geomancy.Color.SRGB: toLinear :: Float -> Float
+ Geomancy.Color.SRGB: toLinear3 :: Vec3 -> Vec3
+ Geomancy.Color.SRGB: toLinear4 :: Vec4 -> Vec4
+ Geomancy.Color.SRGB: xyzToLinear :: Vec3 -> Vec3

Files

ChangeLog.md view
@@ -1,5 +1,24 @@ # Changelog for geomancy +## 0.4.1.0++- Added `Geomancy.Color` with general colour helpers.+  * `premultiply` multiplies the colour channels of a `Vec4` by its alpha.+  * `toUnorm8` clamps a channel to 0..1 and rounds it to a byte. NaN becomes 0. `fromUnorm8` turns a byte back into a channel.+  * Pass `toUnorm8` to the existing `Vec3.convert` or `Vec4.convert` to turn whole vectors into bytes.+- Added `Geomancy.Color.SRGB` with the sRGB transfer functions.+  * `toLinear` and `fromLinear` work on one channel. Negative values mirror the curve, like in CSS Color 4.+  * `toLinear3`, `fromLinear3`, `toLinear4` and `fromLinear4` work on `Vec3` and `Vec4`. Alpha passes through unchanged.+  * Web colour labels like `#F3F1F7` give sRGB-encoded values. `toLinear4` turns them into linear ones.+  * `luminance`, `linearToXYZ` and `xyzToLinear` take linear values. Their matrices are derived from the sRGB primaries and the D65 white point.+- Added `Geomancy.Color.Hue` with HSV and HSL conversions for `Vec3`.+  * Hue is measured in turns. Red is 0, green is 1/3 and blue is 2/3.+  * `hue` gives the fully saturated colour for a hue.+  * `toHSV` and `toHSL` convert from RGB. `fromHSV` and `fromHSL` convert back.+- Added `Geomancy.Color.OKLab` with the OKLab and OKLCh perceptual colour spaces.+  * `fromLinear` turns linear sRGB into OKLab. `toLinear` turns OKLab back into linear sRGB.+  * `toLCh` and `fromLCh` convert between OKLab and its polar OKLCh form. OKLCh hue is measured in turns, like in `Geomancy.Color.Hue`.+ ## 0.4.0.0  A generally safe upgrade, but quite a bit of changes across the board.
geomancy.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack  name:           geomancy-version:        0.4.0.0+version:        0.4.1.0 synopsis:       Vectors and matrix manipulation description:    Sometimes it is unavoidable you have to do stuff on CPU.                 Let's at least do it faster.@@ -35,6 +35,10 @@ library   exposed-modules:       Geomancy+      Geomancy.Color+      Geomancy.Color.Hue+      Geomancy.Color.OKLab+      Geomancy.Color.SRGB       Geomancy.DMat4       Geomancy.DMat4.Internal       Geomancy.DQuaternion@@ -129,6 +133,7 @@   type: exitcode-stdio-1.0   main-is: Main.hs   other-modules:+      Spec.Color       Spec.DMat4       Spec.DQuaternion       Spec.DTransform
+ src/Geomancy/Color.hs view
@@ -0,0 +1,27 @@+{-# LANGUAGE BlockArguments #-}++module Geomancy.Color+  ( premultiply+  , toUnorm8+  , fromUnorm8+  ) where++import Data.Word (Word8)+import Geomancy.Gl.Funs (glSaturate)+import Geomancy.Vec4 (Vec4, vec4, withVec4)++{-# INLINE premultiply #-}+premultiply :: Vec4 -> Vec4+premultiply color =+  withVec4 color \r g b a ->+    vec4 (r * a) (g * a) (b * a) a++{-# INLINE toUnorm8 #-}+toUnorm8 :: Float -> Word8+toUnorm8 channel+  | isNaN channel = 0+  | otherwise = fromIntegral (round (glSaturate channel * 255) :: Int)++{-# INLINE fromUnorm8 #-}+fromUnorm8 :: Word8 -> Float+fromUnorm8 byte = fromIntegral byte / 255
+ src/Geomancy/Color/Hue.hs view
@@ -0,0 +1,69 @@+{-# LANGUAGE BlockArguments #-}++module Geomancy.Color.Hue+  ( hue+  , fromHSV+  , toHSV+  , fromHSL+  , toHSL+  ) where++import Geomancy.Gl.Funs (glFract, glMix, glSaturate)+import Geomancy.Vec3 (Vec3, vec3, withVec3, (^*))++{-# INLINE hue #-}+hue :: Float -> Vec3+hue turns =+  glSaturate (abs (glFract (vec3 turns (turns + 2 / 3) (turns + 1 / 3)) * 6 - 3) - 1)++{-# INLINE fromHSV #-}+fromHSV :: Vec3 -> Vec3+fromHSV hsv =+  withVec3 hsv \h s v ->+    glMix 1 (hue h) s ^* v++{-# INLINE toHSV #-}+toHSV :: Vec3 -> Vec3+toHSV rgb =+  withHue rgb \h hi lo ->+    vec3 h (if hi == 0 then 0 else (hi - lo) / hi) hi++{-# INLINE fromHSL #-}+fromHSL :: Vec3 -> Vec3+fromHSL hsl =+  withVec3 hsl \h s l ->+    vec3 l l l + (hue h - 0.5) ^* (2 * s * min l (1 - l))++{-# INLINE toHSL #-}+toHSL :: Vec3 -> Vec3+toHSL rgb =+  withHue rgb \h hi lo ->+    let+      l = (hi + lo) / 2+      headroom = min l (1 - l)+    in+      vec3 h (if headroom == 0 then 0 else (hi - l) / headroom) l++{-# INLINE withHue #-}+withHue :: Vec3 -> (Float -> Float -> Float -> r) -> r+withHue rgb f =+  withVec3 rgb \r g b ->+    let+      hi = max r (max g b)+      lo = min r (min g b)+      chroma = hi - lo+      sector+        | chroma == 0 = 0+        | hi == r = (g - b) / chroma+        | hi == g = 2 + (b - r) / chroma+        | otherwise = 4 + (r - g) / chroma+    in+      f (wrapTurns (sector / 6)) hi lo++{-# INLINE wrapTurns #-}+wrapTurns :: Float -> Float+wrapTurns x =+  let+    turns = glFract x+  in+    if turns >= 1 then 0 else turns
+ src/Geomancy/Color/OKLab.hs view
@@ -0,0 +1,82 @@+{-# LANGUAGE BlockArguments #-}++module Geomancy.Color.OKLab+  ( fromLinear+  , toLinear+  , toLCh+  , fromLCh+  ) where++import Geomancy.Elementwise (Elementwise(..))+import Geomancy.Gl.Funs (glFract)+import Geomancy.Vec3 (Vec3, dot, vec3, withVec3)++{-# INLINE fromLinear #-}+fromLinear :: Vec3 -> Vec3+fromLinear = lmsToLab . emap signedCubeRoot . linearToLms++{-# INLINE toLinear #-}+toLinear :: Vec3 -> Vec3+toLinear lab = lmsToLinear (lms * lms * lms)+  where+    lms = labToLms lab++{-# INLINE toLCh #-}+toLCh :: Vec3 -> Vec3+toLCh lab =+  withVec3 lab \l a b ->+    vec3 l (sqrt (a * a + b * b)) (wrapTurns (atan2 b a / (2 * pi)))++{-# INLINE fromLCh #-}+fromLCh :: Vec3 -> Vec3+fromLCh lch =+  withVec3 lch \l chroma turns ->+    vec3 l (chroma * cos (2 * pi * turns)) (chroma * sin (2 * pi * turns))++{-# INLINE wrapTurns #-}+wrapTurns :: Float -> Float+wrapTurns x =+  let+    turns = glFract x+  in+    if turns >= 1 then 0 else turns++{-# INLINE signedCubeRoot #-}+signedCubeRoot :: Float -> Float+signedCubeRoot x = signum x * abs x ** (1 / 3)++{-# INLINE byRows #-}+byRows :: Vec3 -> Vec3 -> Vec3 -> Vec3 -> Vec3+byRows r0 r1 r2 v = vec3 (dot r0 v) (dot r1 v) (dot r2 v)++{-# INLINE linearToLms #-}+linearToLms :: Vec3 -> Vec3+linearToLms =+  byRows+    (vec3 0.4122214695 0.5363325373 0.0514459933)+    (vec3 0.2119034958 0.6806995506 0.1073969535)+    (vec3 0.0883024592 0.2817188391 0.6299787017)++{-# INLINE lmsToLinear #-}+lmsToLinear :: Vec3 -> Vec3+lmsToLinear =+  byRows+    (vec3 4.0767416361 (-3.3077115393) 0.2309699032)+    (vec3 (-1.2684379733) 2.6097573493 (-0.3413193760))+    (vec3 (-0.0041960761) (-0.7034186179) 1.7076146941)++{-# INLINE lmsToLab #-}+lmsToLab :: Vec3 -> Vec3+lmsToLab =+  byRows+    (vec3 0.2104542683 0.7936177747 (-0.0040720430))+    (vec3 1.9779985324 (-2.4285922421) 0.4505937096)+    (vec3 0.0259040425 0.7827717125 (-0.8086757549))++{-# INLINE labToLms #-}+labToLms :: Vec3 -> Vec3+labToLms =+  byRows+    (vec3 1 0.3963377774 0.2158037573)+    (vec3 1 (-0.1055613458) (-0.0638541728))+    (vec3 1 (-0.0894841775) (-1.2914855480))
+ src/Geomancy/Color/SRGB.hs view
@@ -0,0 +1,88 @@+{-# LANGUAGE BlockArguments #-}++module Geomancy.Color.SRGB+  ( toLinear+  , fromLinear+  , toLinear3+  , fromLinear3+  , toLinear4+  , fromLinear4+  , luminance+  , linearToXYZ+  , xyzToLinear+  ) where++import Geomancy.Vec3 (Vec3, dot, vec3, withVec3)+import Geomancy.Vec4 (Vec4, vec4, withVec4)++{-# INLINE toLinear #-}+toLinear :: Float -> Float+toLinear encoded+  | abs encoded <= 0.04045 = encoded / 12.92+  | otherwise = signum encoded * ((abs encoded + 0.055) / 1.055) ** 2.4++{-# INLINE fromLinear #-}+fromLinear :: Float -> Float+fromLinear linear+  | abs linear <= 0.0031308 = linear * 12.92+  | otherwise = signum linear * (1.055 * abs linear ** (1 / 2.4) - 0.055)++{-# INLINE toLinear3 #-}+toLinear3 :: Vec3 -> Vec3+toLinear3 color =+  withVec3 color \r g b ->+    vec3 (toLinear r) (toLinear g) (toLinear b)++{-# INLINE fromLinear3 #-}+fromLinear3 :: Vec3 -> Vec3+fromLinear3 color =+  withVec3 color \r g b ->+    vec3 (fromLinear r) (fromLinear g) (fromLinear b)++{-# INLINE toLinear4 #-}+toLinear4 :: Vec4 -> Vec4+toLinear4 color =+  withVec4 color \r g b a ->+    vec4 (toLinear r) (toLinear g) (toLinear b) a++{-# INLINE fromLinear4 #-}+fromLinear4 :: Vec4 -> Vec4+fromLinear4 color =+  withVec4 color \r g b a ->+    vec4 (fromLinear r) (fromLinear g) (fromLinear b) a++{-# INLINE luminance #-}+luminance :: Vec3 -> Float+luminance = dot yRow++{-# INLINE linearToXYZ #-}+linearToXYZ :: Vec3 -> Vec3+linearToXYZ rgb = vec3 (dot xRow rgb) (dot yRow rgb) (dot zRow rgb)++{-# INLINE xyzToLinear #-}+xyzToLinear :: Vec3 -> Vec3+xyzToLinear xyz = vec3 (dot rRow xyz) (dot gRow xyz) (dot bRow xyz)++{-# INLINE xRow #-}+xRow :: Vec3+xRow = vec3 0.4123907993 0.3575843394 0.1804807884++{-# INLINE yRow #-}+yRow :: Vec3+yRow = vec3 0.2126390059 0.7151686788 0.0721923154++{-# INLINE zRow #-}+zRow :: Vec3+zRow = vec3 0.0193308187 0.1191947798 0.9505321522++{-# INLINE rRow #-}+rRow :: Vec3+rRow = vec3 3.2409699419 (-1.5373831776) (-0.4986107603)++{-# INLINE gRow #-}+gRow :: Vec3+gRow = vec3 (-0.9692436363) 1.8759675015 0.0415550574++{-# INLINE bRow #-}+bRow :: Vec3+bRow = vec3 0.0556300797 (-0.2039769589) 1.0569715142
test/Main.hs view
@@ -4,6 +4,7 @@  import Test.Tasty (defaultMain, testGroup) +import Spec.Color qualified import Spec.DMat4 qualified import Spec.DQuaternion qualified import Spec.DTransform qualified@@ -35,4 +36,5 @@   , Spec.Storable.tests   , Spec.Fusion.tests   , Spec.Swizzle.tests+  , Spec.Color.tests   ]
+ test/Spec/Color.hs view
@@ -0,0 +1,273 @@+{-# LANGUAGE BlockArguments #-}+{-# LANGUAGE ImportQualifiedPost #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE PatternSynonyms #-}++module Spec.Color (tests) where++import Data.Foldable (for_)+import Data.Word (Word8)+import Hedgehog+import Hedgehog.Gen qualified as Gen+import Hedgehog.Range qualified as Range+import Test.Tasty (TestTree, testGroup)+import Test.Tasty.Hedgehog (testPropertyNamed)++import Geomancy.Color qualified as Color+import Geomancy.Color.Hue qualified as Hue+import Geomancy.Color.OKLab qualified as OKLab+import Geomancy.Color.SRGB qualified as SRGB+import Geomancy.Vec3 (Vec3, vec3, withVec3)+import Geomancy.Vec3 qualified as Vec3+import Geomancy.Vec4 (vec4)+import Geomancy.Vec4 qualified as Vec4++import Support.Property (pattern PROP_TESTS)++tests :: TestTree+tests = testGroup "Color"+  [ testPropertyNamed "srgb_reference" "prop_srgb_reference" prop_srgb_reference+  , testPropertyNamed "srgb_roundtrip" "prop_srgb_roundtrip" prop_srgb_roundtrip+  , testPropertyNamed "srgb_mirrors_negatives" "prop_srgb_mirrors_negatives" prop_srgb_mirrors_negatives+  , testPropertyNamed "srgb_monotonic" "prop_srgb_monotonic" prop_srgb_monotonic+  , testPropertyNamed "srgb_vec3" "prop_srgb_vec3" prop_srgb_vec3+  , testPropertyNamed "srgb_vec4_keeps_alpha" "prop_srgb_vec4_keeps_alpha" prop_srgb_vec4_keeps_alpha+  , testPropertyNamed "premultiply_scales_rgb" "prop_premultiply_scales_rgb" prop_premultiply_scales_rgb+  , testPropertyNamed "premultiply_clear" "prop_premultiply_clear" prop_premultiply_clear+  , testPropertyNamed "srgb_xyz_white" "prop_srgb_xyz_white" prop_srgb_xyz_white+  , testPropertyNamed "srgb_xyz_roundtrip" "prop_srgb_xyz_roundtrip" prop_srgb_xyz_roundtrip+  , testPropertyNamed "srgb_luminance_is_y" "prop_srgb_luminance_is_y" prop_srgb_luminance_is_y+  , testPropertyNamed "hue_reference" "prop_hue_reference" prop_hue_reference+  , testPropertyNamed "hue_piecewise" "prop_hue_piecewise" prop_hue_piecewise+  , testPropertyNamed "hsv_roundtrip" "prop_hsv_roundtrip" prop_hsv_roundtrip+  , testPropertyNamed "hsl_roundtrip" "prop_hsl_roundtrip" prop_hsl_roundtrip+  , testPropertyNamed "hsl_dark_edge" "prop_hsl_dark_edge" prop_hsl_dark_edge+  , testPropertyNamed "unorm8_bytes" "prop_unorm8_bytes" prop_unorm8_bytes+  , testPropertyNamed "unorm8_clamps" "prop_unorm8_clamps" prop_unorm8_clamps+  , testPropertyNamed "oklab_reference" "prop_oklab_reference" prop_oklab_reference+  , testPropertyNamed "oklab_ottosson_table" "prop_oklab_ottosson_table" prop_oklab_ottosson_table+  , testPropertyNamed "oklab_grey" "prop_oklab_grey" prop_oklab_grey+  , testPropertyNamed "oklab_roundtrip" "prop_oklab_roundtrip" prop_oklab_roundtrip+  , testPropertyNamed "oklch_roundtrip" "prop_oklch_roundtrip" prop_oklch_roundtrip+  ]++unit :: Gen Float+unit = Gen.float (Range.linearFrac 0 1)++rgb :: Gen Vec3+rgb = vec3 <$> unit <*> unit <*> unit++close :: Float -> Float -> PropertyT IO ()+close = closeWithin 1e-5++closeWithin :: Float -> Float -> Float -> PropertyT IO ()+closeWithin tolerance expected actual = diff (abs (actual - expected)) (<) tolerance++closeVec3 :: Vec3 -> Vec3 -> PropertyT IO ()+closeVec3 = closeVec3Within 1e-5++closeVec3Within :: Float -> Vec3 -> Vec3 -> PropertyT IO ()+closeVec3Within tolerance expected actual =+  withVec3 expected \x y z ->+    withVec3 actual \x' y' z' -> do+      closeWithin tolerance x x'+      closeWithin tolerance y y'+      closeWithin tolerance z z'++prop_srgb_reference :: Property+prop_srgb_reference = withTests 1 $ property do+  SRGB.toLinear 0 === 0+  SRGB.toLinear 1 === 1+  SRGB.fromLinear 0 === 0+  close 1 (SRGB.fromLinear 1)+  close 0.2140411 (SRGB.toLinear 0.5)+  close 0.7353570 (SRGB.fromLinear 0.5)+  SRGB.toLinear 0.04045 === 0.04045 / 12.92+  close (-0.2140411) (SRGB.toLinear (-0.5))+  close (-0.7353570) (SRGB.fromLinear (-0.5))++prop_srgb_roundtrip :: Property+prop_srgb_roundtrip = withTests PROP_TESTS $ property do+  x <- forAll unit+  close x (SRGB.fromLinear (SRGB.toLinear x))+  close x (SRGB.toLinear (SRGB.fromLinear x))++prop_srgb_mirrors_negatives :: Property+prop_srgb_mirrors_negatives = withTests PROP_TESTS $ property do+  x <- forAll unit+  SRGB.toLinear (negate x) === negate (SRGB.toLinear x)+  SRGB.fromLinear (negate x) === negate (SRGB.fromLinear x)++prop_srgb_monotonic :: Property+prop_srgb_monotonic = withTests PROP_TESTS $ property do+  a <- forAll unit+  b <- forAll unit+  let+    lo = min a b+    hi = max a b+  diff (SRGB.toLinear lo) (<=) (SRGB.toLinear hi)+  diff (SRGB.fromLinear lo) (<=) (SRGB.fromLinear hi + fromLinearThresholdDip)++fromLinearThresholdDip :: Float+fromLinearThresholdDip = 1e-7++prop_srgb_vec3 :: Property+prop_srgb_vec3 = withTests PROP_TESTS $ property do+  r <- forAll unit+  g <- forAll unit+  b <- forAll unit+  SRGB.toLinear3 (vec3 r g b) === vec3 (SRGB.toLinear r) (SRGB.toLinear g) (SRGB.toLinear b)+  SRGB.fromLinear3 (vec3 r g b) === vec3 (SRGB.fromLinear r) (SRGB.fromLinear g) (SRGB.fromLinear b)++prop_srgb_vec4_keeps_alpha :: Property+prop_srgb_vec4_keeps_alpha = withTests PROP_TESTS $ property do+  r <- forAll unit+  g <- forAll unit+  b <- forAll unit+  a <- forAll unit+  SRGB.toLinear4 (vec4 r g b a) === vec4 (SRGB.toLinear r) (SRGB.toLinear g) (SRGB.toLinear b) a+  SRGB.fromLinear4 (vec4 r g b a) === vec4 (SRGB.fromLinear r) (SRGB.fromLinear g) (SRGB.fromLinear b) a++prop_premultiply_scales_rgb :: Property+prop_premultiply_scales_rgb = withTests PROP_TESTS $ property do+  r <- forAll unit+  g <- forAll unit+  b <- forAll unit+  a <- forAll unit+  Color.premultiply (vec4 r g b a) === vec4 (r * a) (g * a) (b * a) a++prop_premultiply_clear :: Property+prop_premultiply_clear = withTests 1 $ property do+  Color.premultiply (vec4 1 0.5 0.25 0) === vec4 0 0 0 0++d65 :: Vec3+d65 = vec3 0.9504559 1 1.0890578++prop_srgb_xyz_white :: Property+prop_srgb_xyz_white = withTests 1 $ property do+  closeVec3 d65 (SRGB.linearToXYZ 1)+  closeVec3 1 (SRGB.xyzToLinear d65)+  close 1 (SRGB.luminance 1)++prop_srgb_xyz_roundtrip :: Property+prop_srgb_xyz_roundtrip = withTests PROP_TESTS $ property do+  color <- forAll rgb+  closeVec3 color (SRGB.xyzToLinear (SRGB.linearToXYZ color))++prop_srgb_luminance_is_y :: Property+prop_srgb_luminance_is_y = withTests PROP_TESTS $ property do+  color <- forAll rgb+  withVec3 (SRGB.linearToXYZ color) \_x y _z ->+    SRGB.luminance color === y++prop_hue_reference :: Property+prop_hue_reference = withTests 1 $ property do+  closeVec3 (vec3 1 0 0) (Hue.hue 0)+  closeVec3 (vec3 1 1 0) (Hue.hue (1 / 6))+  closeVec3 (vec3 0 1 0) (Hue.hue (1 / 3))+  closeVec3 (vec3 0 1 1) (Hue.hue (1 / 2))+  closeVec3 (vec3 0 0 1) (Hue.hue (2 / 3))+  closeVec3 (vec3 1 0 1) (Hue.hue (5 / 6))+  closeVec3 (vec3 1 0 0) (Hue.hue 1)+  closeVec3 (vec3 0 0 1) (Hue.hue (-1 / 3))+  Hue.toHSV (vec3 1 0 0) === vec3 0 1 1+  Hue.toHSL (vec3 1 0 0) === vec3 0 1 0.5+  Hue.toHSV (vec3 0.5 0.5 0.5) === vec3 0 0 0.5+  Hue.toHSL (vec3 0.5 0.5 0.5) === vec3 0 0 0.5+  Hue.toHSV (vec3 1 0 1e-9) === vec3 0 1 1+  Hue.toHSL (vec3 1 0 1e-9) === vec3 0 1 0.5+  closeVec3 (vec3 0.4 0.8 0.4) (Hue.fromHSV (vec3 (1 / 3) 0.5 0.8))+  closeVec3 (vec3 (1 / 3) 0.5 0.8) (Hue.toHSV (vec3 0.4 0.8 0.4))+  closeVec3 (vec3 0.2 0.2 0.6) (Hue.fromHSL (vec3 (2 / 3) 0.5 0.4))+  closeVec3 (vec3 (2 / 3) 0.5 0.4) (Hue.toHSL (vec3 0.2 0.2 0.6))++prop_hue_piecewise :: Property+prop_hue_piecewise = withTests PROP_TESTS $ property do+  turns <- forAll unit+  closeVec3 (piecewiseHue turns) (Hue.hue turns)++piecewiseHue :: Float -> Vec3+piecewiseHue turns+  | sector <= 1 = vec3 1 rising 0+  | sector <= 2 = vec3 rising 1 0+  | sector <= 3 = vec3 0 1 rising+  | sector <= 4 = vec3 0 rising 1+  | sector <= 5 = vec3 rising 0 1+  | otherwise = vec3 1 0 rising+  where+    sector = turns * 6+    rising = 1 - abs (sector - 2 * fromIntegral (floor (sector / 2) :: Int) - 1)++prop_hsv_roundtrip :: Property+prop_hsv_roundtrip = withTests PROP_TESTS $ property do+  color <- forAll rgb+  closeVec3 color (Hue.fromHSV (Hue.toHSV color))++prop_hsl_roundtrip :: Property+prop_hsl_roundtrip = withTests PROP_TESTS $ property do+  color <- forAll rgb+  closeVec3 color (Hue.fromHSL (Hue.toHSL color))++prop_hsl_dark_edge :: Property+prop_hsl_dark_edge = withTests 1 $ property do+  let dark = vec3 1e-8 0 0+  Hue.toHSL dark === vec3 0 1 5e-9+  closeVec3 dark (Hue.fromHSL (Hue.toHSL dark))++prop_unorm8_bytes :: Property+prop_unorm8_bytes = withTests 1 $ property do+  for_ [minBound .. maxBound :: Word8] \byte ->+    Color.toUnorm8 (Color.fromUnorm8 byte) === byte++prop_unorm8_clamps :: Property+prop_unorm8_clamps = withTests 1 $ property do+  Color.toUnorm8 (-1) === 0+  Color.toUnorm8 2 === 255+  Color.toUnorm8 (0 / 0) === 0+  Color.toUnorm8 (SRGB.fromLinear 1) === 255+  Vec3.convert Color.toUnorm8 (,,) (vec3 0 0.5 1) === (0, 128, 255)+  Vec4.convert Color.toUnorm8 (,,,) (vec4 1 0.5 0 2) === (255, 128, 0, 255)++prop_oklab_reference :: Property+prop_oklab_reference = withTests 1 $ property do+  closeVec3 (vec3 1 0 0) (OKLab.fromLinear 1)+  closeVec3 (vec3 0.6279554 0.2248631 0.1258463) (OKLab.fromLinear (vec3 1 0 0))+  closeVec3 (vec3 0.6279554 0.2576833 0.0812052) (OKLab.toLCh (OKLab.fromLinear (vec3 1 0 0)))+  closeVec3 (vec3 0.5 0.1 0.25) (OKLab.toLCh (vec3 0.5 0 0.1))+  closeVec3 (vec3 0.5 0 0.1) (OKLab.fromLCh (vec3 0.5 0.1 0.25))+  OKLab.toLCh (vec3 0.5 1 (-1e-9)) === vec3 0.5 1 0++prop_oklab_ottosson_table :: Property+prop_oklab_ottosson_table = withTests 1 $ property do+  for_ ottossonPairs \(xyz, lab) ->+    closeVec3Within ottossonTablePrecision lab (OKLab.fromLinear (SRGB.xyzToLinear xyz))++ottossonPairs :: [(Vec3, Vec3)]+ottossonPairs =+  [ (vec3 0.950 1.000 1.089, vec3 1.000 0.000 0.000)+  , (vec3 1 0 0, vec3 0.450 1.236 (-0.019))+  , (vec3 0 1 0, vec3 0.922 (-0.671) 0.263)+  , (vec3 0 0 1, vec3 0.153 (-1.415) (-0.449))+  ]++ottossonTablePrecision :: Float+ottossonTablePrecision = 1e-3++prop_oklab_grey :: Property+prop_oklab_grey = withTests PROP_TESTS $ property do+  grey <- forAll unit+  closeVec3 (vec3 (grey ** (1 / 3)) 0 0) (OKLab.fromLinear (vec3 grey grey grey))++prop_oklab_roundtrip :: Property+prop_oklab_roundtrip = withTests PROP_TESTS $ property do+  color <- forAll $ vec3 <$> outOfGamut <*> outOfGamut <*> outOfGamut+  closeVec3 color (OKLab.toLinear (OKLab.fromLinear color))+  where+    outOfGamut = Gen.float (Range.linearFrac (-0.5) 1.5)++prop_oklch_roundtrip :: Property+prop_oklch_roundtrip = withTests PROP_TESTS $ property do+  color <- forAll rgb+  let lab = OKLab.fromLinear color+  closeVec3 lab (OKLab.fromLCh (OKLab.toLCh lab))+  closeVec3 color (OKLab.toLinear (OKLab.fromLCh (OKLab.toLCh lab)))