903_colorcvt.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. exit()
  2. import colorcvt
  3. from vmath import vec3
  4. def oklch(expr: str) -> vec3:
  5. # oklch(82.33% 0.37 153)
  6. expr = expr[6:-1]
  7. l, c, h = expr.split()
  8. l = float(l[:-1]) / 100
  9. return vec3(l, float(c), float(h))
  10. def srgb32(expr: str) -> vec3:
  11. # rgb(0, 239, 115)
  12. expr = expr[4:-1]
  13. r, g, b = expr.split(", ")
  14. r, g, b = int(r), int(g), int(b)
  15. c = vec3(r, g, b) / 255
  16. return colorcvt.srgb_to_linear_srgb(c)
  17. def assert_equal(title: str, a: vec3, b: vec3) -> None:
  18. epsilon = 1e-3
  19. try:
  20. assert abs(a.x - b.x) < epsilon
  21. assert abs(a.y - b.y) < epsilon
  22. assert abs(a.z - b.z) < epsilon
  23. except AssertionError:
  24. raise AssertionError(f"{title}\nExpected: {b}, got: {a}")
  25. def test(oklch_expr: str, srgb32_expr: str) -> None:
  26. oklch_color = oklch(oklch_expr)
  27. srgb32_color = srgb32(srgb32_expr)
  28. assert_equal('oklch_to_linear_srgb', colorcvt.oklch_to_linear_srgb(oklch_color), srgb32_color)
  29. # in range check
  30. oklch_color = colorcvt.linear_srgb_to_oklch(srgb32_color)
  31. assert_equal('oklch_to_linear_srgb+', colorcvt.oklch_to_linear_srgb(oklch_color), srgb32_color)
  32. assert_equal('linear_srgb_to_oklch+', colorcvt.linear_srgb_to_oklch(srgb32_color), oklch_color)
  33. test("oklch(71.32% 0.1381 153)", "rgb(83, 187, 120)")
  34. test("oklch(45.15% 0.037 153)", "rgb(70, 92, 76)")
  35. test("oklch(22.5% 0.0518 153)", "rgb(4, 34, 16)")
  36. test("oklch(100% 0.37 153)", "rgb(255, 255, 255)")
  37. test("oklch(0% 0.0395 283.24)", "rgb(0, 0, 0)")
  38. # hard samples
  39. # test("oklch(95% 0.2911 264.18)", "rgb(224, 239, 255)")
  40. # test("oklch(28.09% 0.2245 153)", "rgb(0, 54, 12)")
  41. # test("oklch(82.33% 0.37 153)", "rgb(0, 239, 115)")