hsl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkTypeface.h"
  14. #include "include/core/SkTypes.h"
  15. #include "tools/ToolUtils.h"
  16. // Hue, Saturation, Color, and Luminosity blend modes are oddballs.
  17. // They nominally convert their inputs to unpremul, then to HSL, then
  18. // mix-and-match H,S,and L from Src and Dst, then convert back, then premul.
  19. //
  20. // In practice that's slow, so instead we pick the color with the correct
  21. // Hue, and then (approximately) apply the other's Saturation and/or Luminosity.
  22. // This isn't just an optimization... it's how the modes are specified.
  23. //
  24. // Each mode's name describes the Src H,S,L components to keep, taking the
  25. // others from Dst, where Color == Hue + Saturation. Color and Luminosity
  26. // are each other's complements; Hue and Saturation have no complement.
  27. //
  28. // All these modes were originally designed to operate on gamma-encoded values,
  29. // and that's what everyone's used to seeing. It's unclear wehther they make
  30. // any sense in a gamma-correct world. They certainly won't look at all similar.
  31. //
  32. // We have had many inconsistent implementations of these modes.
  33. // This GM tries to demonstrate unambigously how they should work.
  34. //
  35. // To go along with our inconsistent implementations, there are two slightly
  36. // inconsistent specifications of how to perform these blends,
  37. // web: https://www.w3.org/TR/compositing-1/#blendingnonseparable
  38. // KHR: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_blend_equation_advanced.txt
  39. // It looks like these are meant to be identical, but they disagree on at least ClipColor().
  40. //
  41. // I think the KHR version is just wrong... it produces values >1. So we use the web version.
  42. static float min(float r, float g, float b) { return SkTMin(r, SkTMin(g, b)); }
  43. static float max(float r, float g, float b) { return SkTMax(r, SkTMax(g, b)); }
  44. static float sat(float r, float g, float b) { return max(r,g,b) - min(r,g,b); }
  45. static float lum(float r, float g, float b) { return r*0.30f + g*0.59f + b*0.11f; }
  46. // The two SetSat() routines in the specs look different, but they're logically equivalent.
  47. // Both map the minimum channel to 0, maximum to s, and scale the middle proportionately.
  48. // The KHR version has done a better job at simplifying its math, so we use it here.
  49. static void set_sat(float* r, float* g, float* b, float s) {
  50. float mn = min(*r,*g,*b),
  51. mx = max(*r,*g,*b);
  52. auto channel = [=](float c) {
  53. return mx == mn ? 0
  54. : (c - mn) * s / (mx - mn);
  55. };
  56. *r = channel(*r);
  57. *g = channel(*g);
  58. *b = channel(*b);
  59. }
  60. static void clip_color(float* r, float* g, float* b) {
  61. float l = lum(*r,*g,*b),
  62. mn = min(*r,*g,*b),
  63. mx = max(*r,*g,*b);
  64. auto clip = [=](float c) {
  65. if (mn < 0) { c = l + (c - l) * ( l) / (l - mn); }
  66. if (mx > 1) { c = l + (c - l) * (1 - l) / (mx - l); }
  67. SkASSERT(-0.0001f < c); // This may end up very slightly negative...
  68. SkASSERT( c <= 1);
  69. return c;
  70. };
  71. *r = clip(*r);
  72. *g = clip(*g);
  73. *b = clip(*b);
  74. }
  75. static void set_lum(float* r, float* g, float* b, float l) {
  76. float diff = l - lum(*r,*g,*b);
  77. *r += diff;
  78. *g += diff;
  79. *b += diff;
  80. clip_color(r,g,b);
  81. }
  82. static void hue(float dr, float dg, float db,
  83. float* sr, float* sg, float* sb) {
  84. // Hue of Src, Saturation and Luminosity of Dst.
  85. float R = *sr,
  86. G = *sg,
  87. B = *sb;
  88. set_sat(&R,&G,&B, sat(dr,dg,db));
  89. set_lum(&R,&G,&B, lum(dr,dg,db));
  90. *sr = R;
  91. *sg = G;
  92. *sb = B;
  93. }
  94. static void saturation(float dr, float dg, float db,
  95. float* sr, float* sg, float* sb) {
  96. // Saturation of Src, Hue and Luminosity of Dst
  97. float R = dr,
  98. G = dg,
  99. B = db;
  100. set_sat(&R,&G,&B, sat(*sr,*sg,*sb));
  101. set_lum(&R,&G,&B, lum( dr, dg, db)); // This may seem redundant, but it is not.
  102. *sr = R;
  103. *sg = G;
  104. *sb = B;
  105. }
  106. static void color(float dr, float dg, float db,
  107. float* sr, float* sg, float* sb) {
  108. // Hue and Saturation of Src, Luminosity of Dst.
  109. float R = *sr,
  110. G = *sg,
  111. B = *sb;
  112. set_lum(&R,&G,&B, lum(dr,dg,db));
  113. *sr = R;
  114. *sg = G;
  115. *sb = B;
  116. }
  117. static void luminosity(float dr, float dg, float db,
  118. float* sr, float* sg, float* sb) {
  119. // Luminosity of Src, Hue and Saturation of Dst.
  120. float R = dr,
  121. G = dg,
  122. B = db;
  123. set_lum(&R,&G,&B, lum(*sr,*sg,*sb));
  124. *sr = R;
  125. *sg = G;
  126. *sb = B;
  127. }
  128. static SkColor blend(SkColor dst, SkColor src,
  129. void (*mode)(float,float,float, float*,float*,float*)) {
  130. SkASSERT(SkColorGetA(dst) == 0xff
  131. && SkColorGetA(src) == 0xff); // Not fundamental, just simplifying for this GM.
  132. SkColor4f d = SkColor4f::FromColor(dst),
  133. s = SkColor4f::FromColor(src);
  134. mode( d.fR, d.fG, d.fB,
  135. &s.fR, &s.fG, &s.fB);
  136. return s.toSkColor();
  137. }
  138. DEF_SIMPLE_GM(hsl, canvas, 600, 100) {
  139. SkPaint paint;
  140. SkFont font(ToolUtils::create_portable_typeface());
  141. const char* comment = "HSL blend modes are correct when you see no circles in the squares.";
  142. canvas->drawString(comment, 10,10, font, paint);
  143. // Just to keep things reaaaal simple, we'll only use opaque colors.
  144. SkPaint bg, fg;
  145. bg.setColor(0xff00ff00); // Fully-saturated bright green, H = 120°, S = 100%, L = 50%.
  146. fg.setColor(0xff7f3f7f); // Partly-saturated dim magenta, H = 300°, S = ~33%, L = ~37%.
  147. struct {
  148. SkBlendMode mode;
  149. void (*reference)(float,float,float, float*,float*,float*);
  150. } tests[] = {
  151. { SkBlendMode::kSrc, nullptr },
  152. { SkBlendMode::kDst, nullptr },
  153. { SkBlendMode::kHue, hue },
  154. { SkBlendMode::kSaturation, saturation },
  155. { SkBlendMode::kColor, color },
  156. { SkBlendMode::kLuminosity, luminosity },
  157. };
  158. for (auto test : tests) {
  159. canvas->drawRect({20,20,80,80}, bg);
  160. fg.setBlendMode(test.mode);
  161. canvas->drawRect({20,20,80,80}, fg);
  162. if (test.reference) {
  163. SkPaint ref;
  164. ref.setColor(blend(bg.getColor(), fg.getColor(), test.reference));
  165. canvas->drawCircle(50,50, 20, ref);
  166. }
  167. canvas->drawString(SkBlendMode_Name(test.mode), 20, 90, font, paint);
  168. canvas->translate(100,0);
  169. }
  170. }