packages feed

geomancy-0.4.1.0 / test / Spec / Color.hs

{-# LANGUAGE BlockArguments #-}{-# LANGUAGE ImportQualifiedPost #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE PatternSynonyms #-}module Spec.Color (tests) whereimport Data.Foldable (for_)import Data.Word (Word8)import Hedgehogimport Hedgehog.Gen qualified as Genimport Hedgehog.Range qualified as Rangeimport Test.Tasty (TestTree, testGroup)import Test.Tasty.Hedgehog (testPropertyNamed)import Geomancy.Color qualified as Colorimport Geomancy.Color.Hue qualified as Hueimport Geomancy.Color.OKLab qualified as OKLabimport Geomancy.Color.SRGB qualified as SRGBimport Geomancy.Vec3 (Vec3, vec3, withVec3)import Geomancy.Vec3 qualified as Vec3import Geomancy.Vec4 (vec4)import Geomancy.Vec4 qualified as Vec4import Support.Property (pattern PROP_TESTS)tests :: TestTreetests = 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 Floatunit = Gen.float (Range.linearFrac 0 1)rgb :: Gen Vec3rgb = vec3 <$> unit <*> unit <*> unitclose :: Float -> Float -> PropertyT IO ()close = closeWithin 1e-5closeWithin :: Float -> Float -> Float -> PropertyT IO ()closeWithin tolerance expected actual = diff (abs (actual - expected)) (<) tolerancecloseVec3 :: Vec3 -> Vec3 -> PropertyT IO ()closeVec3 = closeVec3Within 1e-5closeVec3Within :: 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 :: Propertyprop_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 :: Propertyprop_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 :: Propertyprop_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 :: Propertyprop_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 :: FloatfromLinearThresholdDip = 1e-7prop_srgb_vec3 :: Propertyprop_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 :: Propertyprop_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) aprop_premultiply_scales_rgb :: Propertyprop_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) aprop_premultiply_clear :: Propertyprop_premultiply_clear = withTests 1 $ property do  Color.premultiply (vec4 1 0.5 0.25 0) === vec4 0 0 0 0d65 :: Vec3d65 = vec3 0.9504559 1 1.0890578prop_srgb_xyz_white :: Propertyprop_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 :: Propertyprop_srgb_xyz_roundtrip = withTests PROP_TESTS $ property do  color <- forAll rgb  closeVec3 color (SRGB.xyzToLinear (SRGB.linearToXYZ color))prop_srgb_luminance_is_y :: Propertyprop_srgb_luminance_is_y = withTests PROP_TESTS $ property do  color <- forAll rgb  withVec3 (SRGB.linearToXYZ color) \_x y _z ->    SRGB.luminance color === yprop_hue_reference :: Propertyprop_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 :: Propertyprop_hue_piecewise = withTests PROP_TESTS $ property do  turns <- forAll unit  closeVec3 (piecewiseHue turns) (Hue.hue turns)piecewiseHue :: Float -> Vec3piecewiseHue 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 :: Propertyprop_hsv_roundtrip = withTests PROP_TESTS $ property do  color <- forAll rgb  closeVec3 color (Hue.fromHSV (Hue.toHSV color))prop_hsl_roundtrip :: Propertyprop_hsl_roundtrip = withTests PROP_TESTS $ property do  color <- forAll rgb  closeVec3 color (Hue.fromHSL (Hue.toHSL color))prop_hsl_dark_edge :: Propertyprop_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 :: Propertyprop_unorm8_bytes = withTests 1 $ property do  for_ [minBound .. maxBound :: Word8] \byte ->    Color.toUnorm8 (Color.fromUnorm8 byte) === byteprop_unorm8_clamps :: Propertyprop_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 :: Propertyprop_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 0prop_oklab_ottosson_table :: Propertyprop_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 :: FloatottossonTablePrecision = 1e-3prop_oklab_grey :: Propertyprop_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 :: Propertyprop_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 :: Propertyprop_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)))