Beatmup
matrix.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../basic_types.h"
21 
22 namespace Beatmup {
23  namespace Color {
24 
25  /**
26  RGBA color mapping.
27  A color value is mapped onto another color value by multiplying the 4x4 matrix by an RGBA input column.
28  */
29  class Matrix : public Object {
30  public:
31 
32  /**
33  Matrix elements
34  */
35  union {
36  float elem[4][4];
37  struct {
39  };
40  };
41 #ifdef BEATMUP_CHANNEL_ORDER_ARGB
42  inline color4f a() const { return rows[0]; }
43  inline color4f r() const { return rows[1]; }
44  inline color4f g() const { return rows[2]; }
45  inline color4f b() const { return rows[3]; }
46  inline color4f& a() { return rows[0]; }
47  inline color4f& r() { return rows[1]; }
48  inline color4f& g() { return rows[2]; }
49  inline color4f& b() { return rows[3]; }
50 #elif BEATMUP_CHANNEL_ORDER_BGRA
51  inline color4f b() const { return rows[0]; }
52  inline color4f g() const { return rows[1]; }
53  inline color4f r() const { return rows[2]; }
54  inline color4f a() const { return rows[3]; }
55  inline color4f& b() { return rows[0]; }
56  inline color4f& g() { return rows[1]; }
57  inline color4f& r() { return rows[2]; }
58  inline color4f& a() { return rows[3]; }
59 #else // RGBA
60  inline color4f r() const { return rows[0]; }
61  inline color4f g() const { return rows[1]; }
62  inline color4f b() const { return rows[2]; }
63  inline color4f a() const { return rows[3]; }
64  inline color4f& r() { return rows[0]; }
65  inline color4f& g() { return rows[1]; }
66  inline color4f& b() { return rows[2]; }
67  inline color4f& a() { return rows[3]; }
68 #endif
69 
70  /**
71  Initializes the color matrix to identity.
72  */
73  Matrix();
74 
75  /**
76  Constructs a matrix from four RGBA color values setting them as matrix rows.
77  \param r First row of the color matrix.
78  \param g Second row of the color matrix.
79  \param b Third row of the color matrix.
80  \param a Fourth row of the color matrix.
81  */
82  Matrix(const color4f& r, const color4f& g, const color4f& b, const color4f& a);
83 
84  /**
85  Constructs a color matrix representing an HSV correction transformation.
86  \param hDegrees hue offset in degrees (additive)
87  \param saturationFactor saturation scaling factor
88  \param valueFactor value scaling factor
89  */
90  Matrix(float hDegrees, float saturationFactor = 1.0f, float valueFactor = 1.0f);
91 
92  /**
93  Constructs a color matrix representing continuous color inversion with a fixed hue point.
94  \param preservedColor color giving a hue value that remains unchanged by the transform.
95  \param saturationFactor saturation scaling factor
96  \param valueFactor value scaling factor
97  */
98  Matrix(const color3f& preservedColor, float saturationFactor = 1.0f, float valueFactor = 1.0f);
99 
100  /**
101  Retrieves matrix rows by index in 0..3 range.
102  */
103  color4f& operator[](int);
104  color4f operator[](int) const;
105 
106  /**
107  Computes the right-multiplication of the current Matrix and another Matrix.
108  */
109  Matrix operator*(const Matrix&) const;
110 
111  void operator=(const Matrix&);
112  };
113  }
114 }
RGBA color mapping.
Definition: matrix.h:29
float elem[4][4]
Definition: matrix.h:36
Matrix()
Initializes the color matrix to identity.
Definition: matrix.cpp:28
color4f & r()
Definition: matrix.h:64
color4f r() const
Definition: matrix.h:60
color4f g() const
Definition: matrix.h:61
color4f & a()
Definition: matrix.h:67
void operator=(const Matrix &)
Definition: matrix.cpp:64
color4f & b()
Definition: matrix.h:66
color4f rows[4]
Definition: matrix.h:38
Matrix operator*(const Matrix &) const
Computes the right-multiplication of the current Matrix and another Matrix.
Definition: matrix.cpp:52
color4f b() const
Definition: matrix.h:62
color4f & operator[](int)
Retrieves matrix rows by index in 0..3 range.
Definition: matrix.cpp:42
color4f & g()
Definition: matrix.h:65
color4f a() const
Definition: matrix.h:63
Beatmup object base class
Definition: basic_types.h:67
Beatmup::color3f preservedColor