DeltaE.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <AK/Math.h>
  8. #include <LibGfx/DeltaE.h>
  9. #include <math.h>
  10. namespace Gfx {
  11. float DeltaE(CIELAB const& c1, CIELAB const& c2)
  12. {
  13. // https://en.wikipedia.org/wiki/Color_difference#CIEDE2000
  14. // http://zschuessler.github.io/DeltaE/learn/
  15. // https://www.hajim.rochester.edu/ece/sites/gsharma/ciede2000/ciede2000noteCRNA.pdf
  16. float delta_L_prime = c2.L - c1.L;
  17. float L_bar = (c1.L + c2.L) / 2;
  18. float C1 = hypotf(c1.a, c1.b);
  19. float C2 = hypotf(c2.a, c2.b);
  20. float C_bar = (C1 + C2) / 2;
  21. float G = 0.5f * (1 - sqrtf(powf(C_bar, 7) / (powf(C_bar, 7) + powf(25, 7))));
  22. float a1_prime = (1 + G) * c1.a;
  23. float a2_prime = (1 + G) * c2.a;
  24. float C1_prime = hypotf(a1_prime, c1.b);
  25. float C2_prime = hypotf(a2_prime, c2.b);
  26. float C_prime_bar = (C1_prime + C2_prime) / 2;
  27. float delta_C_prime = C2_prime - C1_prime;
  28. auto h_prime = [](float b, float a_prime) {
  29. if (b == 0 && a_prime == 0)
  30. return 0.f;
  31. float h_prime = atan2(b, a_prime);
  32. if (h_prime < 0)
  33. h_prime += 2 * static_cast<float>(M_PI);
  34. return AK::to_degrees(h_prime);
  35. };
  36. float h1_prime = h_prime(c1.b, a1_prime);
  37. float h2_prime = h_prime(c2.b, a2_prime);
  38. float delta_h_prime;
  39. if (C1_prime == 0 || C2_prime == 0)
  40. delta_h_prime = 0;
  41. else if (fabsf(h1_prime - h2_prime) <= 180.f)
  42. delta_h_prime = h2_prime - h1_prime;
  43. else if (h2_prime <= h1_prime)
  44. delta_h_prime = h2_prime - h1_prime + 360;
  45. else
  46. delta_h_prime = h2_prime - h1_prime - 360;
  47. auto sin_degrees = [](float x) { return sinf(AK::to_radians(x)); };
  48. auto cos_degrees = [](float x) { return cosf(AK::to_radians(x)); };
  49. float delta_H_prime = 2 * sqrtf(C1_prime * C2_prime) * sin_degrees(delta_h_prime / 2);
  50. float h_prime_bar;
  51. if (C1_prime == 0 || C2_prime == 0)
  52. h_prime_bar = h1_prime + h2_prime;
  53. else if (fabsf(h1_prime - h2_prime) <= 180.f)
  54. h_prime_bar = (h1_prime + h2_prime) / 2;
  55. else if (h1_prime + h2_prime < 360)
  56. h_prime_bar = (h1_prime + h2_prime + 360) / 2;
  57. else
  58. h_prime_bar = (h1_prime + h2_prime - 360) / 2;
  59. float T = 1 - 0.17f * cos_degrees(h_prime_bar - 30) + 0.24f * cos_degrees(2 * h_prime_bar) + 0.32f * cos_degrees(3 * h_prime_bar + 6) - 0.2f * cos_degrees(4 * h_prime_bar - 63);
  60. float S_L = 1 + 0.015f * powf(L_bar - 50, 2) / sqrtf(20 + powf(L_bar - 50, 2));
  61. float S_C = 1 + 0.045f * C_prime_bar;
  62. float S_H = 1 + 0.015f * C_prime_bar * T;
  63. float R_T = -2 * sqrtf(powf(C_prime_bar, 7) / (powf(C_prime_bar, 7) + powf(25, 7))) * sin_degrees(60 * exp(-powf((h_prime_bar - 275) / 25, 2)));
  64. // "kL, kC, and kH are usually unity."
  65. float k_L = 1, k_C = 1, k_H = 1;
  66. float L = delta_L_prime / (k_L * S_L);
  67. float C = delta_C_prime / (k_C * S_C);
  68. float H = delta_H_prime / (k_H * S_H);
  69. return sqrtf(powf(L, 2) + powf(C, 2) + powf(H, 2) + R_T * C * H);
  70. }
  71. }