Index: ui/gfx/color_utils.cc |
diff --git a/ui/gfx/color_utils.cc b/ui/gfx/color_utils.cc |
index d64a8b3636059cf5c79c714ff39e0983d2ddba9d..d7149bae22aec455deb45efc5c58df45107957b6 100644 |
--- a/ui/gfx/color_utils.cc |
+++ b/ui/gfx/color_utils.cc |
@@ -23,7 +23,6 @@ |
namespace color_utils { |
- |
// Helper functions ----------------------------------------------------------- |
namespace { |
@@ -64,6 +63,89 @@ SkColor LightnessInvertColor(SkColor color) { |
// ---------------------------------------------------------------------------- |
+Matrix4x3::Matrix4x3() { |
+ for (int row = 0; row < 3; row++) { |
+ for (int col = 0; col < 4; col++) { |
+ rows[row][col] = row == col ? 1.0f : 0.0f; |
+ } |
+ } |
+} |
+ |
+float Matrix4x3::lookup(int row, int col) const { |
+ if (row == 3) |
+ return col == 3 ? 1.0f : 0.0; |
+ return rows[row][col]; |
+} |
+ |
+Matrix4x3 Matrix4x3::operator*(const Matrix4x3& other) const { |
+ Matrix4x3 ret; |
+ for (int col = 0; col < 4; col++) { |
+ for (int row = 0; row < 3; row++) { |
+ float sum = 0.0; |
+ for (int i = 0; i < 4; i++) { |
+ sum += lookup(i, col) * other.lookup(row, i); |
+ } |
+ ret.rows[row][col] = sum; |
+ } |
+ } |
+ return ret; |
+} |
+ |
+TriStim Matrix4x3::operator*(const TriStim& color) const { |
+ TriStim ret; |
+ ret.values[0] = rows[0][0] * color.values[0] + rows[0][1] * color.values[1] + |
+ rows[0][2] * color.values[2] + rows[0][3]; |
+ ret.values[1] = rows[1][0] * color.values[0] + rows[1][1] * color.values[1] + |
+ rows[1][2] * color.values[2] + rows[1][3]; |
+ ret.values[2] = rows[2][0] * color.values[0] + rows[2][1] * color.values[1] + |
+ rows[2][2] * color.values[2] + rows[2][3]; |
+ return ret; |
+} |
+ |
+Matrix4x3 Matrix4x3::Invert() const { |
+ Matrix4x3 ret, m(*this); |
+ for (int col = 0; col < 4; col++) { |
+ if (m.rows[col][col] != 1.0) { |
+ int best = col; |
+ for (int row = col + 1; row < 3; row++) { |
+ if (fabs(m.rows[row][col]) > fabs(m.rows[best][col])) { |
+ best = row; |
+ } |
+ } |
+ if (best != col) { |
+ // Swap rows x, best |
+ std::swap(m.rows[col], m.rows[best]); |
+ std::swap(ret.rows[col], ret.rows[best]); |
+ } |
+ if (m.rows[col][col] == 0.0f) { |
+ // Failed, shouldn't happen. |
+ NOTREACHED(); |
+ return ret; |
+ } |
+ float mult = 1.0f / m.rows[col][col]; |
+ for (int col2 = 0; col2 < 4; col2++) { |
+ m.rows[col][col2] *= mult; |
+ ret.rows[col][col2] *= mult; |
+ } |
+ m.rows[col][col] = 1.0f; |
+ } |
+ for (int row = 0; row < 3; row++) { |
+ if (row == col) |
+ continue; |
+ float mult = m.rows[row][col]; |
+ if (mult == 0.0f) |
+ continue; |
+ |
+ for (int col2 = 0; col2 < 4; col2++) { |
+ m.rows[row][col2] -= m.rows[col][col2] * mult; |
+ ret.rows[row][col2] -= ret.rows[col][col2] * mult; |
+ } |
+ m.rows[row][col] = 0.0f; |
+ } |
+ } |
+ return ret; |
+} |
+ |
double GetContrastRatio(SkColor color_a, SkColor color_b) { |
return GetContrastRatio(GetRelativeLuminance(color_a), |
GetRelativeLuminance(color_b)); |