diff options
-rw-r--r-- | ui/gfx/canvas_skia_win.cc | 23 | ||||
-rw-r--r-- | ui/gfx/color_utils.cc | 18 | ||||
-rw-r--r-- | ui/gfx/color_utils.h | 4 |
3 files changed, 22 insertions, 23 deletions
diff --git a/ui/gfx/canvas_skia_win.cc b/ui/gfx/canvas_skia_win.cc index 048cbf6..9024673 100644 --- a/ui/gfx/canvas_skia_win.cc +++ b/ui/gfx/canvas_skia_win.cc @@ -13,6 +13,7 @@ #include "skia/ext/bitmap_platform_device.h" #include "skia/ext/skia_utils_win.h" #include "third_party/skia/include/core/SkShader.h" +#include "ui/gfx/color_utils.h" #include "ui/gfx/font.h" #include "ui/gfx/rect.h" @@ -209,8 +210,9 @@ void DrawTextAndClearBackground(skia::BitmapPlatformDevice& bmp_device, // Gets the color directly. DrawText doesn't premultiply alpha so // using SkBitmap::getColor() won't work here. SkColor color = *bmp.getAddr32(x, y); - // Use any of the color channels as alpha. - BYTE alpha = 0xFF - SkColorGetB(color); + // Calculate the alpha using the luminance. Since this is black text + // on a white background the luminosity must be inverted. + BYTE alpha = 0xFF - color_utils::GetLuminanceForColor(color); *bmp.getAddr32(x, y) = SkPreMultiplyColor( SkColorSetARGB(alpha, text_color_r, text_color_b, text_color_g)); } @@ -550,13 +552,6 @@ void CanvasSkia::DrawFadeTruncatingString( text_rect.set_x(text_rect.x() - offset_x); text_rect.set_width(text_rect.width() + offset_x); - // Disable clear type. This makes the text render in gray scale which - // makes it easier to compute the alpha value. - LOGFONT font_info; - GetObject(font.GetNativeFont(), sizeof(font_info), &font_info); - font_info.lfQuality = ANTIALIASED_QUALITY; - base::win::ScopedHFONT gray_scale_font(CreateFontIndirect(&font_info)); - // Create a temporary bitmap to draw the gradient to. scoped_ptr<skia::BitmapPlatformDevice> gradient_bitmap( skia::BitmapPlatformDevice::create( @@ -565,11 +560,13 @@ void CanvasSkia::DrawFadeTruncatingString( HDC hdc = beginPlatformPaint(); if (is_truncating_head) - DrawTextGradientPart(hdc, *gradient_bitmap, text, color, gray_scale_font, - text_rect, head_part, is_rtl, flags); + DrawTextGradientPart(hdc, *gradient_bitmap, text, color, + font.GetNativeFont(), text_rect, head_part, is_rtl, + flags); if (is_truncating_tail) - DrawTextGradientPart(hdc, *gradient_bitmap, text, color, gray_scale_font, - text_rect, tail_part, !is_rtl, flags); + DrawTextGradientPart(hdc, *gradient_bitmap, text, color, + font.GetNativeFont(), text_rect, tail_part, !is_rtl, + flags); endPlatformPaint(); // Draw the solid part. diff --git a/ui/gfx/color_utils.cc b/ui/gfx/color_utils.cc index 3dacb44..eb639f9 100644 --- a/ui/gfx/color_utils.cc +++ b/ui/gfx/color_utils.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -43,13 +43,6 @@ int calcHue(double temp1, double temp2, double hue) { return static_cast<int>(result * 255 + .5); } -int GetLumaForColor(SkColor* color) { - int luma = static_cast<int>((0.3 * SkColorGetR(*color)) + - (0.59 * SkColorGetG(*color)) + - (0.11 * SkColorGetB(*color))); - return std::max(std::min(luma, 255), 0); -} - // Next two functions' formulas from: // http://www.w3.org/TR/WCAG20/#relativeluminancedef // http://www.w3.org/TR/WCAG20/#contrast-ratiodef @@ -79,6 +72,13 @@ double ContrastRatio(double foreground_luminance, double background_luminance) { // ---------------------------------------------------------------------------- +unsigned char GetLuminanceForColor(SkColor color) { + int luma = static_cast<int>((0.3 * SkColorGetR(color)) + + (0.59 * SkColorGetG(color)) + + (0.11 * SkColorGetB(color))); + return std::max(std::min(luma, 255), 0); +} + double RelativeLuminance(SkColor color) { return (0.2126 * ConvertSRGB(SkColorGetR(color))) + (0.7152 * ConvertSRGB(SkColorGetG(color))) + @@ -249,7 +249,7 @@ void BuildLumaHistogram(SkBitmap* bitmap, int histogram[256]) { for (int y = 0; y < pixel_height; ++y) { SkColor* current_color = static_cast<uint32_t*>(bitmap->getAddr32(0, y)); for (int x = 0; x < pixel_width; ++x, ++current_color) - histogram[GetLumaForColor(current_color)]++; + histogram[GetLuminanceForColor(*current_color)]++; } } diff --git a/ui/gfx/color_utils.h b/ui/gfx/color_utils.h index a043b82..86eb055 100644 --- a/ui/gfx/color_utils.h +++ b/ui/gfx/color_utils.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -19,6 +19,8 @@ struct HSL { double l; }; +unsigned char GetLuminanceForColor(SkColor color); + // Calculated according to http://www.w3.org/TR/WCAG20/#relativeluminancedef double RelativeLuminance(SkColor color); |