Skip to content

Commit

Permalink
Move functions from skia/ext to app/gfx where possible: most of skia_…
Browse files Browse the repository at this point in the history
…utils.* and image_operations.* can be moved because they are not used by WebKit code.

This also fixes the spelling of "Convolusion" to "Convolution" and updates some copyrights.

This is a re-do of r26975, this time with WebKit update and some fixes to compile on Mac and Linux.

BUG=none
TEST=none

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27031 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
pkasting@chromium.org committed Sep 24, 2009
1 parent 37cc1a1 commit cab34d6
Show file tree
Hide file tree
Showing 39 changed files with 1,109 additions and 1,211 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vars = {
"webkit_trunk":
"http://svn.webkit.org/repository/webkit/trunk",
"webkit_revision": "48679",
"webkit_revision": "48684",
"ffmpeg_revision": "26428",
}

Expand Down
4 changes: 4 additions & 0 deletions app/app.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
'gfx/path_gtk.cc',
'gfx/path_win.cc',
'gfx/path.h',
'gfx/skbitmap_operations.cc',
'gfx/skbitmap_operations.h',
'gfx/text_elider.cc',
'gfx/text_elider.h',
'gtk_dnd_util.cc',
Expand Down Expand Up @@ -199,8 +201,10 @@
],
'sources': [
'animation_unittest.cc',
'gfx/color_utils_unittest.cc',
'gfx/font_unittest.cc',
'gfx/icon_util_unittest.cc',
'gfx/skbitmap_operations_unittest.cc',
'gfx/text_elider_unittest.cc',
'l10n_util_mac_unittest.mm',
'l10n_util_unittest.cc',
Expand Down
119 changes: 119 additions & 0 deletions app/gfx/color_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ namespace color_utils {

namespace {

double calcHue(double temp1, double temp2, double hue) {
if (hue < 0.0)
++hue;
else if (hue > 1.0)
--hue;

if (hue * 6.0 < 1.0)
return temp1 + (temp2 - temp1) * hue * 6.0;
if (hue * 2.0 < 1.0)
return temp2;
if (hue * 3.0 < 2.0)
return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hue) * 6.0;

return temp1;
}

int GetLumaForColor(SkColor* color) {
int luma = static_cast<int>((0.3 * SkColorGetR(*color)) +
(0.59 * SkColorGetG(*color)) +
Expand Down Expand Up @@ -56,6 +72,109 @@ double ContrastRatio(SkColor color1, SkColor color2) {

// ----------------------------------------------------------------------------

void SkColorToHSL(SkColor c, HSL* hsl) {
double r = static_cast<double>(SkColorGetR(c)) / 255.0;
double g = static_cast<double>(SkColorGetG(c)) / 255.0;
double b = static_cast<double>(SkColorGetB(c)) / 255.0;
double vmax = std::max(std::max(r, g), b);
double vmin = std::min(std::min(r, g), b);
double delta = vmax - vmin;
hsl->l = (vmax + vmin) / 2;
if (delta) {
double dr = (((vmax - r) / 6.0) + (delta / 2.0)) / delta;
double dg = (((vmax - g) / 6.0) + (delta / 2.0)) / delta;
double db = (((vmax - b) / 6.0) + (delta / 2.0)) / delta;
if (r == vmax)
hsl->h = db - dg;
else if (g == vmax)
hsl->h = (1.0 / 3.0) + dr - db;
else if (b == vmax)
hsl->h = (2.0 / 3.0) + dg - dr;

if (hsl->h < 0.0)
++hsl->h;
else if (hsl->h > 1.0)
--hsl->h;

hsl->s = delta / ((hsl->l < 0.5) ? (vmax + vmin) : (2 - vmax - vmin));
} else {
hsl->h = hsl->s = 0;
}
}

SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha) {
double hue = hsl.h;
double saturation = hsl.s;
double lightness = hsl.l;

// If there's no color, we don't care about hue and can do everything based
// on brightness.
if (!saturation) {
uint8 light;

if (lightness < 0)
light = 0;
else if (lightness >= 1.0)
light = 255;
else
light = SkDoubleToFixed(lightness) >> 8;

return SkColorSetARGB(alpha, light, light, light);
}

double temp2 = (lightness < 0.5) ?
(lightness * (1.0 + saturation)) :
(lightness + saturation - (lightness * saturation));
double temp1 = 2.0 * lightness - temp2;
return SkColorSetARGB(alpha,
static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * 255),
static_cast<int>(calcHue(temp1, temp2, hue) * 255),
static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * 255));
}

SkColor HSLShift(SkColor color, const 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;

// Change the saturation.
if (shift.s >= 0) {
if (shift.s <= 0.5)
hsl.s *= shift.s * 2.0;
else
hsl.s += (1.0 - hsl.s) * ((shift.s - 0.5) * 2.0);
}

SkColor result = HSLToSkColor(hsl, alpha);

if (shift.l < 0)
return result;

// Lightness shifts in the style of popular image editors aren't
// actually represented in HSL - the L value does have some effect
// on saturation.
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) {
r *= (shift.l * 2.0);
g *= (shift.l * 2.0);
b *= (shift.l * 2.0);
} else {
r += (255.0 - r) * ((shift.l - 0.5) * 2.0);
g += (255.0 - g) * ((shift.l - 0.5) * 2.0);
b += (255.0 - b) * ((shift.l - 0.5) * 2.0);
}
return SkColorSetARGB(alpha,
static_cast<int>(r),
static_cast<int>(g),
static_cast<int>(b));
}

bool IsColorCloseToTransparent(SkAlpha alpha) {
const int kCloseToBoundary = 64;
return alpha < kCloseToBoundary;
Expand Down
27 changes: 27 additions & 0 deletions app/gfx/color_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ class SkBitmap;

namespace color_utils {

// Represents an HSL color.
struct HSL {
double h;
double s;
double l;
};

// Note: these transformations assume sRGB as the source color space
void SkColorToHSL(SkColor c, HSL* hsl);
SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha);

// HSL-Shift an SkColor. 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(SkColor color, const HSL& shift);

// Determine if a given alpha value is nearly completely transparent.
bool IsColorCloseToTransparent(SkAlpha alpha);

Expand Down
24 changes: 10 additions & 14 deletions skia/ext/skia_utils_unittest.cc → app/gfx/color_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,32 @@

#include <stdlib.h>

#include "skia/ext/skia_utils.h"
#include "app/gfx/color_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColorPriv.h"

TEST(SkiaUtils, SkColorToHSLRed) {
SkColor red = SkColorSetARGB(255, 255, 0, 0);
skia::HSL hsl = { 0, 0, 0 };
skia::SkColorToHSL(red, hsl);
TEST(ColorUtils, SkColorToHSLRed) {
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(SK_ColorRED, &hsl);
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);
skia::HSL hsl = { 0, 0, 0 };
skia::SkColorToHSL(red, hsl);
TEST(ColorUtils, SkColorToHSLGrey) {
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
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.
}

TEST(SkiaUtils, HSLToSkColorWithAlpha) {
TEST(ColorUtils, HSLToSkColorWithAlpha) {
SkColor red = SkColorSetARGB(128, 255, 0, 0);

skia::HSL hsl = { 0, 1, 0.5 };

SkColor result = skia::HSLToSkColor(128, hsl);
color_utils::HSL hsl = { 0, 1, 0.5 };
SkColor result = color_utils::HSLToSkColor(hsl, 128);
EXPECT_EQ(SkColorGetA(red), SkColorGetA(result));
EXPECT_EQ(SkColorGetR(red), SkColorGetR(result));
EXPECT_EQ(SkColorGetG(red), SkColorGetG(result));
Expand Down
Loading

0 comments on commit cab34d6

Please sign in to comment.