summaryrefslogtreecommitdiffstats
path: root/skia/ext/skia_utils.cc
diff options
context:
space:
mode:
authorglen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-15 20:35:13 +0000
committerglen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-15 20:35:13 +0000
commitfb23b9b370b689a954fb42b0beea2e61eee9e159 (patch)
treeeec1d826f89a34d48de7868ca34fe05b8d2d9fc4 /skia/ext/skia_utils.cc
parent0aad67b662f0ba306eeea71117d77bccefc3533f (diff)
downloadchromium_src-fb23b9b370b689a954fb42b0beea2e61eee9e159.zip
chromium_src-fb23b9b370b689a954fb42b0beea2e61eee9e159.tar.gz
chromium_src-fb23b9b370b689a954fb42b0beea2e61eee9e159.tar.bz2
Make our HSL shifting match Photoshop's.
Also clean up a bunch of PMColor code - skia_utils should operate on SkColors and PMColors should only ever be used by SkBitmaps and ImageOperations. BUG=16687 Review URL: http://codereview.chromium.org/149663 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20788 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/skia_utils.cc')
-rw-r--r--skia/ext/skia_utils.cc48
1 files changed, 33 insertions, 15 deletions
diff --git a/skia/ext/skia_utils.cc b/skia/ext/skia_utils.cc
index 5a1226e..84a003b 100644
--- a/skia/ext/skia_utils.cc
+++ b/skia/ext/skia_utils.cc
@@ -38,11 +38,10 @@ static inline double calcHue(double temp1, double temp2, double hueVal) {
return temp1;
}
-SkPMColor HSLToSKColor(U8CPU alpha, HSL hsl) {
+SkColor HSLToSkColor(U8CPU alpha, HSL hsl) {
double hue = hsl.h;
double saturation = hsl.s;
double lightness = hsl.l;
- double scaleFactor = 256.0;
// If there's no color, we don't care about hue and can do everything based
// on brightness.
@@ -56,8 +55,7 @@ SkPMColor HSLToSKColor(U8CPU alpha, HSL hsl) {
else
light = SkDoubleToFixed(lightness) >> 8;
- unsigned greyValue = SkAlphaMul(light, alpha);
- return SkColorSetARGB(alpha, greyValue, greyValue, greyValue);
+ return SkColorSetARGB(alpha, light, light, light);
}
double temp2 = (lightness < 0.5) ?
@@ -70,12 +68,12 @@ SkPMColor HSLToSKColor(U8CPU alpha, HSL hsl) {
double bh = calcHue(temp1, temp2, hue - 1.0 / 3.0);
return SkColorSetARGB(alpha,
- SkAlphaMul(static_cast<int>(rh * scaleFactor), alpha),
- SkAlphaMul(static_cast<int>(gh * scaleFactor), alpha),
- SkAlphaMul(static_cast<int>(bh * scaleFactor), alpha));
+ static_cast<int>(rh * 255),
+ static_cast<int>(gh * 255),
+ static_cast<int>(bh * 255));
}
-void SkColorToHSL(SkPMColor c, HSL& hsl) {
+void SkColorToHSL(SkColor c, HSL& hsl) {
double r = SkColorGetR(c) / 255.0;
double g = SkColorGetG(c) / 255.0;
double b = SkColorGetB(c) / 255.0;
@@ -119,7 +117,11 @@ void SkColorToHSL(SkPMColor c, HSL& hsl) {
hsl.l = l;
}
-SkColor HSLShift(HSL hsl, HSL shift) {
+SkColor HSLShift(SkColor color, HSL shift) {
+ HSL hsl;
+ int alpha = SkColorGetA(color);
+ SkColorToHSL(color, hsl);
+
// Replace the hue with the tint's hue.
if (shift.h >= 0)
hsl.h = shift.h;
@@ -134,17 +136,33 @@ SkColor HSLShift(HSL hsl, HSL shift) {
}
}
- // Change the lightness.
+ SkColor result = HSLToSkColor(alpha, hsl);
+
+ // Lightness shifts in the style of popular image editors aren't
+ // actually represented in HSL - the L value does have some effect
+ // on saturation.
if (shift.l >= 0) {
+ double r = static_cast<double>SkColorGetR(result);
+ double g = static_cast<double>SkColorGetG(result);
+ double b = static_cast<double>SkColorGetB(result);
+
if (shift.l <= 0.5) {
- hsl.l *= shift.l * 2.0;
+ r *= (shift.l * 2.0);
+ g *= (shift.l * 2.0);
+ b *= (shift.l * 2.0);
} else {
- hsl.l = hsl.l + (1.0 - hsl.l) *
- ((shift.l - 0.5) * 2.0);
+ r = (r + (255.0 - r) * ((shift.l - 0.5) * 2.0));
+ g = (g + (255.0 - g) * ((shift.l - 0.5) * 2.0));
+ b = (b + (255.0 - b) * ((shift.l - 0.5) * 2.0));
}
- }
- return skia::HSLToSKColor(0xff, hsl);
+ return SkColorSetARGB(alpha,
+ static_cast<int>(r),
+ static_cast<int>(g),
+ static_cast<int>(b));
+ } else {
+ return result;
+ }
}