OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/color_utils.h" | 5 #include "ui/gfx/color_utils.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "ui/gfx/color_palette.h" | 16 #include "ui/gfx/color_palette.h" |
17 #include "ui/gfx/geometry/safe_integer_conversions.h" | 17 #include "ui/gfx/geometry/safe_integer_conversions.h" |
18 | 18 |
19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
20 #include <windows.h> | 20 #include <windows.h> |
21 #include "skia/ext/skia_utils_win.h" | 21 #include "skia/ext/skia_utils_win.h" |
22 #endif | 22 #endif |
23 | 23 |
24 namespace color_utils { | 24 namespace color_utils { |
25 | 25 |
26 | |
27 // Helper functions ----------------------------------------------------------- | 26 // Helper functions ----------------------------------------------------------- |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
31 int calcHue(double temp1, double temp2, double hue) { | 30 int calcHue(double temp1, double temp2, double hue) { |
32 if (hue < 0.0) | 31 if (hue < 0.0) |
33 ++hue; | 32 ++hue; |
34 else if (hue > 1.0) | 33 else if (hue > 1.0) |
35 --hue; | 34 --hue; |
36 | 35 |
(...skipping 20 matching lines...) Expand all Loading... |
57 SkColorToHSL(color, &hsl); | 56 SkColorToHSL(color, &hsl); |
58 hsl.l = 1.0 - hsl.l; | 57 hsl.l = 1.0 - hsl.l; |
59 return HSLToSkColor(hsl, SkColorGetA(color)); | 58 return HSLToSkColor(hsl, SkColorGetA(color)); |
60 } | 59 } |
61 | 60 |
62 } // namespace | 61 } // namespace |
63 | 62 |
64 | 63 |
65 // ---------------------------------------------------------------------------- | 64 // ---------------------------------------------------------------------------- |
66 | 65 |
| 66 Matrix4x3::Matrix4x3() { |
| 67 for (int row = 0; row < 3; row++) { |
| 68 for (int col = 0; col < 4; col++) { |
| 69 rows[row][col] = row == col ? 1.0f : 0.0f; |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 74 float Matrix4x3::lookup(int row, int col) const { |
| 75 if (row == 3) |
| 76 return col == 3 ? 1.0f : 0.0; |
| 77 return rows[row][col]; |
| 78 } |
| 79 |
| 80 Matrix4x3 Matrix4x3::operator*(const Matrix4x3& other) const { |
| 81 Matrix4x3 ret; |
| 82 for (int col = 0; col < 4; col++) { |
| 83 for (int row = 0; row < 3; row++) { |
| 84 float sum = 0.0; |
| 85 for (int i = 0; i < 4; i++) { |
| 86 sum += lookup(i, col) * other.lookup(row, i); |
| 87 } |
| 88 ret.rows[row][col] = sum; |
| 89 } |
| 90 } |
| 91 return ret; |
| 92 } |
| 93 |
| 94 TriStim Matrix4x3::operator*(const TriStim& color) const { |
| 95 TriStim ret; |
| 96 ret.values[0] = rows[0][0] * color.values[0] + rows[0][1] * color.values[1] + |
| 97 rows[0][2] * color.values[2] + rows[0][3]; |
| 98 ret.values[1] = rows[1][0] * color.values[0] + rows[1][1] * color.values[1] + |
| 99 rows[1][2] * color.values[2] + rows[1][3]; |
| 100 ret.values[2] = rows[2][0] * color.values[0] + rows[2][1] * color.values[1] + |
| 101 rows[2][2] * color.values[2] + rows[2][3]; |
| 102 return ret; |
| 103 } |
| 104 |
| 105 Matrix4x3 Matrix4x3::Invert() const { |
| 106 Matrix4x3 ret, m(*this); |
| 107 for (int col = 0; col < 4; col++) { |
| 108 if (m.rows[col][col] != 1.0) { |
| 109 int best = col; |
| 110 for (int row = col + 1; row < 3; row++) { |
| 111 if (fabs(m.rows[row][col]) > fabs(m.rows[best][col])) { |
| 112 best = row; |
| 113 } |
| 114 } |
| 115 if (best != col) { |
| 116 // Swap rows x, best |
| 117 std::swap(m.rows[col], m.rows[best]); |
| 118 std::swap(ret.rows[col], ret.rows[best]); |
| 119 } |
| 120 if (m.rows[col][col] == 0.0f) { |
| 121 // Failed, shouldn't happen. |
| 122 NOTREACHED(); |
| 123 return ret; |
| 124 } |
| 125 float mult = 1.0f / m.rows[col][col]; |
| 126 for (int col2 = 0; col2 < 4; col2++) { |
| 127 m.rows[col][col2] *= mult; |
| 128 ret.rows[col][col2] *= mult; |
| 129 } |
| 130 m.rows[col][col] = 1.0f; |
| 131 } |
| 132 for (int row = 0; row < 3; row++) { |
| 133 if (row == col) |
| 134 continue; |
| 135 float mult = m.rows[row][col]; |
| 136 if (mult == 0.0f) |
| 137 continue; |
| 138 |
| 139 for (int col2 = 0; col2 < 4; col2++) { |
| 140 m.rows[row][col2] -= m.rows[col][col2] * mult; |
| 141 ret.rows[row][col2] -= ret.rows[col][col2] * mult; |
| 142 } |
| 143 m.rows[row][col] = 0.0f; |
| 144 } |
| 145 } |
| 146 return ret; |
| 147 } |
| 148 |
67 double GetContrastRatio(SkColor color_a, SkColor color_b) { | 149 double GetContrastRatio(SkColor color_a, SkColor color_b) { |
68 return GetContrastRatio(GetRelativeLuminance(color_a), | 150 return GetContrastRatio(GetRelativeLuminance(color_a), |
69 GetRelativeLuminance(color_b)); | 151 GetRelativeLuminance(color_b)); |
70 } | 152 } |
71 | 153 |
72 double GetContrastRatio(double luminance_a, double luminance_b) { | 154 double GetContrastRatio(double luminance_a, double luminance_b) { |
73 DCHECK_GE(luminance_a, 0.0); | 155 DCHECK_GE(luminance_a, 0.0); |
74 DCHECK_GE(luminance_b, 0.0); | 156 DCHECK_GE(luminance_b, 0.0); |
75 luminance_a += 0.05; | 157 luminance_a += 0.05; |
76 luminance_b += 0.05; | 158 luminance_b += 0.05; |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 // For black text, this comes out to kChromeIconGrey. | 415 // For black text, this comes out to kChromeIconGrey. |
334 return color_utils::AlphaBlend(SK_ColorWHITE, text_color, | 416 return color_utils::AlphaBlend(SK_ColorWHITE, text_color, |
335 SkColorGetR(gfx::kChromeIconGrey)); | 417 SkColorGetR(gfx::kChromeIconGrey)); |
336 } | 418 } |
337 // For a light color, just reduce opacity. | 419 // For a light color, just reduce opacity. |
338 return SkColorSetA(text_color, | 420 return SkColorSetA(text_color, |
339 static_cast<int>(0.8f * SkColorGetA(text_color))); | 421 static_cast<int>(0.8f * SkColorGetA(text_color))); |
340 } | 422 } |
341 | 423 |
342 } // namespace color_utils | 424 } // namespace color_utils |
OLD | NEW |