Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Unified Diff: ui/gfx/color_utils.cc

Issue 2182633012: Add Matrix4x3 and tristimulus class to color utiliies. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added test Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/color_utils.h ('k') | ui/gfx/color_utils_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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));
« no previous file with comments | « ui/gfx/color_utils.h ('k') | ui/gfx/color_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698