diff options
author | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-09 01:07:42 +0000 |
---|---|---|
committer | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-09 01:07:42 +0000 |
commit | 4a19063d1459a4c9c8c4c50ed86eb9048f69ea3f (patch) | |
tree | 69a1e9f78b3a5fa8b909cfab336826b09c44235f /skia/ext | |
parent | 5085ee0b4bfbe4625e63ee6975bb95702e13e0aa (diff) | |
download | chromium_src-4a19063d1459a4c9c8c4c50ed86eb9048f69ea3f.zip chromium_src-4a19063d1459a4c9c8c4c50ed86eb9048f69ea3f.tar.gz chromium_src-4a19063d1459a4c9c8c4c50ed86eb9048f69ea3f.tar.bz2 |
This is the first pass at themes.
This CL is paired with http://codereview.chromium.org/67284
This CL (for commit purposes) includes http://codereview.chromium.org/67284
BUG=4463,11232,11233,11234,11235
Review URL: http://codereview.chromium.org/99030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15704 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext')
-rw-r--r-- | skia/ext/image_operations.cc | 29 | ||||
-rw-r--r-- | skia/ext/image_operations.h | 3 | ||||
-rw-r--r-- | skia/ext/image_operations_unittest.cc | 4 | ||||
-rw-r--r-- | skia/ext/skia_utils.cc | 58 | ||||
-rw-r--r-- | skia/ext/skia_utils.h | 27 | ||||
-rw-r--r-- | skia/ext/skia_utils_unittest.cc | 22 |
6 files changed, 95 insertions, 48 deletions
diff --git a/skia/ext/image_operations.cc b/skia/ext/image_operations.cc index a6ccec4..07fb97b 100644 --- a/skia/ext/image_operations.cc +++ b/skia/ext/image_operations.cc @@ -16,7 +16,6 @@ #include "SkBitmap.h" #include "skia/ext/convolver.h" #include "skia/include/SkColorPriv.h" -#include "skia/ext/skia_utils.h" namespace skia { @@ -460,7 +459,7 @@ SkBitmap ImageOperations::CreateBlurredBitmap(const SkBitmap& bitmap, // static SkBitmap ImageOperations::CreateHSLShiftedBitmap(const SkBitmap& bitmap, - float hsl_shift[3]) { + HSL hsl_shift) { DCHECK(bitmap.empty() == false); DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); @@ -494,30 +493,30 @@ SkBitmap ImageOperations::CreateHSLShiftedBitmap(const SkBitmap& bitmap, static_cast<int>(b * 255.0)); } - float pixel_hsl[3]; + HSL pixel_hsl = { 0, 0, 0 }; SkColorToHSL(color, pixel_hsl); // Replace the hue with the tint's hue. - if (hsl_shift[0] >= 0) - pixel_hsl[0] = hsl_shift[0]; + if (hsl_shift.h >= 0) + pixel_hsl.h = hsl_shift.h; // Change the saturation. - if (hsl_shift[1] >= 0) { - if (hsl_shift[1] <= 0.5) { - pixel_hsl[1] *= hsl_shift[1] * 2.0; + if (hsl_shift.s >= 0) { + if (hsl_shift.s <= 0.5) { + pixel_hsl.s *= hsl_shift.s * 2.0; } else { - pixel_hsl[1] = pixel_hsl[1] + (1.0 - pixel_hsl[1]) * - ((hsl_shift[1] - 0.5) * 2.0); + pixel_hsl.s = pixel_hsl.s + (1.0 - pixel_hsl.s) * + ((hsl_shift.s - 0.5) * 2.0); } } // Change the lightness. - if (hsl_shift[2] >= 0) { - if (hsl_shift[2] <= 0.5) { - pixel_hsl[2] *= hsl_shift[2] * 2.0; + if (hsl_shift.l >= 0) { + if (hsl_shift.l <= 0.5) { + pixel_hsl.l *= hsl_shift.l * 2.0; } else { - pixel_hsl[2] = pixel_hsl[2] + (1.0 - pixel_hsl[2]) * - ((hsl_shift[2] - 0.5) * 2.0); + pixel_hsl.l = pixel_hsl.l + (1.0 - pixel_hsl.l) * + ((hsl_shift.l - 0.5) * 2.0); } } diff --git a/skia/ext/image_operations.h b/skia/ext/image_operations.h index 5c46b36..b5e049d 100644 --- a/skia/ext/image_operations.h +++ b/skia/ext/image_operations.h @@ -7,6 +7,7 @@ #include "base/basictypes.h" #include "base/gfx/rect.h" +#include "skia/ext/skia_utils.h" #include "SkColor.h" class SkBitmap; @@ -80,7 +81,7 @@ class ImageOperations { // 0.5 = leave unchanged. // 1 = full lightness (make all pixels white). static SkBitmap CreateHSLShiftedBitmap(const SkBitmap& bitmap, - float hsl_shift[3]); + HSL hsl_shift); // Create a bitmap that is cropped from another bitmap. This is special // because it tiles the original bitmap, so your coordinates can extend diff --git a/skia/ext/image_operations_unittest.cc b/skia/ext/image_operations_unittest.cc index 5ed69da..8f7e9be 100644 --- a/skia/ext/image_operations_unittest.cc +++ b/skia/ext/image_operations_unittest.cc @@ -309,7 +309,7 @@ TEST(ImageOperations, CreateHSLShiftedBitmapToSame) { } } - float hsl[3] = { -1, -1, -1 }; + skia::HSL hsl = { -1, -1, -1 }; SkBitmap shifted = skia::ImageOperations::CreateHSLShiftedBitmap(src, hsl); @@ -340,7 +340,7 @@ TEST(ImageOperations, CreateHSLShiftedBitmapHueOnly) { } // Shift to red. - float hsl[3] = { 0, -1, -1 }; + skia::HSL hsl = { 0, -1, -1 }; SkBitmap shifted = skia::ImageOperations::CreateHSLShiftedBitmap(src, hsl); diff --git a/skia/ext/skia_utils.cc b/skia/ext/skia_utils.cc index 1424957..3b5484f 100644 --- a/skia/ext/skia_utils.cc +++ b/skia/ext/skia_utils.cc @@ -39,25 +39,25 @@ static inline double calcHue(double temp1, double temp2, double hueVal) { return temp1; } -SkPMColor HSLToSKColor(U8CPU alpha, float hsl[3]) { - double hue = SkScalarToDouble(hsl[0]); - double saturation = SkScalarToDouble(hsl[1]); - double lightness = SkScalarToDouble(hsl[2]); +SkPMColor 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. if (!saturation) { - U8CPU lightness; + U8CPU light; - if (hsl[2] < 0) - lightness = 0; - else if (hsl[2] >= SK_Scalar1) - lightness = 255; + if (lightness < 0) + light = 0; + else if (lightness >= SK_Scalar1) + light = 255; else - lightness = SkScalarToFixed(hsl[2]) >> 8; + light = SkDoubleToFixed(lightness) >> 8; - unsigned greyValue = SkAlphaMul(lightness, alpha); + unsigned greyValue = SkAlphaMul(light, alpha); return SkColorSetARGB(alpha, greyValue, greyValue, greyValue); } @@ -76,7 +76,7 @@ SkPMColor HSLToSKColor(U8CPU alpha, float hsl[3]) { SkAlphaMul(static_cast<int>(bh * scaleFactor), alpha)); } -void SkColorToHSL(SkPMColor c, float hsl[3]) { +void SkColorToHSL(SkPMColor c, HSL& hsl) { double r = SkColorGetR(c) / 255.0; double g = SkColorGetG(c) / 255.0; double b = SkColorGetB(c) / 255.0; @@ -115,9 +115,37 @@ void SkColorToHSL(SkPMColor c, float hsl[3]) { if (h > 1) h -= 1; } - hsl[0] = h; - hsl[1] = s; - hsl[2] = l; + hsl.h = h; + hsl.s = s; + hsl.l = l; +} + +SkColor HSLShift(HSL hsl, HSL shift) { + // Replace the hue with the tint's hue. + if (shift.h >= 0) + hsl.h = shift.h; + + // Change the saturation. + if (shift.s >= 0) { + if (shift.s <= 0.5) { + hsl.s *= shift.s * 2.0; + } else { + hsl.s = hsl.s + (1.0 - hsl.s) * + ((shift.s - 0.5) * 2.0); + } + } + + // Change the lightness. + if (shift.l >= 0) { + if (shift.l <= 0.5) { + hsl.l *= shift.l * 2.0; + } else { + hsl.l = hsl.l + (1.0 - hsl.l) * + ((shift.l - 0.5) * 2.0); + } + } + + return skia::HSLToSKColor(0xff, hsl); } diff --git a/skia/ext/skia_utils.h b/skia/ext/skia_utils.h index 98fa1b0..0f61b18 100644 --- a/skia/ext/skia_utils.h +++ b/skia/ext/skia_utils.h @@ -10,6 +10,12 @@ namespace skia { +struct HSL { + double h; + double s; + double l; +}; + // Creates a vertical gradient shader. The caller owns the shader. // Example usage to avoid leaks: // paint.setShader(gfx::CreateGradientShader(0, 10, red, blue))->safeUnref(); @@ -22,10 +28,27 @@ SkShader* CreateGradientShader(int start_point, SkColor end_color); // Convert a premultiplied SkColor to a HSL value. -void SkColorToHSL(SkPMColor c, float hsl[3]); +void SkColorToHSL(SkPMColor c, HSL& hsl); // Convert a HSL color to a premultiplied SkColor. -SkPMColor HSLToSKColor(U8CPU alpha, float hsl[3]); +SkPMColor HSLToSKColor(U8CPU alpha, HSL hsl); + +// Shift an HSL value. The shift values are in the range of 0-1, +// with the option to specify -1 for 'no change'. The shift values are +// defined as: +// hsl_shift[0] (hue): The absolute hue value - 0 and 1 map +// to 0 and 360 on the hue color wheel (red). +// hsl_shift[1] (saturation): A saturation shift, with the +// following key values: +// 0 = remove all color. +// 0.5 = leave unchanged. +// 1 = fully saturate the image. +// hsl_shift[2] (lightness): A lightness shift, with the +// following key values: +// 0 = remove all lightness (make all pixels black). +// 0.5 = leave unchanged. +// 1 = full lightness (make all pixels white). +SkColor HSLShift(skia::HSL hsl, skia::HSL shift); } // namespace skia diff --git a/skia/ext/skia_utils_unittest.cc b/skia/ext/skia_utils_unittest.cc index f7b00b1..444a5fc9 100644 --- a/skia/ext/skia_utils_unittest.cc +++ b/skia/ext/skia_utils_unittest.cc @@ -11,20 +11,20 @@ TEST(SkiaUtils, SkColorToHSLRed) { SkColor red = SkColorSetARGB(255, 255, 0, 0); - SkScalar hsl[3]; + skia::HSL hsl = { 0, 0, 0 }; skia::SkColorToHSL(red, hsl); - EXPECT_EQ(hsl[0], 0); - EXPECT_EQ(hsl[1], 1); - EXPECT_EQ(hsl[2], 0.5); + EXPECT_EQ(hsl.h, 0); + EXPECT_EQ(hsl.s, 1); + EXPECT_EQ(hsl.l, 0.5); } TEST(SkiaUtils, SkColorToHSLGrey) { SkColor red = SkColorSetARGB(255, 128, 128, 128); - SkScalar hsl[3]; + skia::HSL hsl = { 0, 0, 0 }; skia::SkColorToHSL(red, hsl); - EXPECT_EQ(hsl[0], 0); - EXPECT_EQ(hsl[1], 0); - EXPECT_EQ(static_cast<int>(hsl[2] * 100), + EXPECT_EQ(hsl.h, 0); + EXPECT_EQ(hsl.s, 0); + EXPECT_EQ(static_cast<int>(hsl.l * 100), static_cast<int>(0.5 * 100)); // Accurate to two decimal places. } @@ -32,11 +32,7 @@ TEST(SkiaUtils, HSLToSkColorWithAlpha) { // Premultiplied alpha - this is full red. SkColor red = SkColorSetARGB(128, 128, 0, 0); - SkScalar hsl[3] = { - SkDoubleToScalar(0), - SkDoubleToScalar(1), - SkDoubleToScalar(0.5), - }; + skia::HSL hsl = { 0, 1, 0.5 }; SkColor result = skia::HSLToSKColor(128, hsl); EXPECT_EQ(SkColorGetA(red), SkColorGetA(result)); |