Beatmup
color_spaces.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 "../bitmap/pixel_arithmetic.h"
21 #include <algorithm>
22 #include <math.h>
23 
24 
25 namespace Beatmup {
26 /**
27  %Color representation and conversion tools
28 */
29 namespace Color {
30 
31  /**
32  HSVA quad (for hue, saturation, value and alpha)
33  */
34  struct hsva_t {
36 
37  /**
38  \brief Constructs an HSVA quad from r, g, b, a values
39  */
41  v = std::max(std::max(r, g), b);
42  float C = v - std::min(std::min(r, g), b);
43  if (C == 0)
44  h = 0;
45  else if (v == r)
46  h = modf((g - b) / C, 6) / 6;
47  else if (v == g)
48  h = ((b - r) / C + 2) / 6;
49  else if (v == b)
50  h = ((r - g) / C + 4) / 6;
51  s = v > 0 ? C / v : 0;
52  }
53 
54 
55  /**
56  \brief Conversion to pixfloat
57  */
58  inline operator pixfloat4() const {
59  const float
60  H = h - (long)h + (h >= 0 ? 0 : 1),
61  C = v*s,
62  X = C*(1 - fabs(modf(H*6, 2) - 1)),
63  m = v - C;
64 
65  pixfloat4 out;
66  if (H < 1.0f / 6) {
67  out.r = C + m; out.g = X + m; out.b = m;
68  }
69  else if (H < 2.0f / 6) {
70  out.r = X + m; out.g = C + m; out.b = m;
71  }
72  else if (H < 3.0f / 6) {
73  out.r = m; out.g = C + m; out.b = X + m;
74  }
75  else if (H < 4.0f / 6) {
76  out.r = m; out.g = X + m; out.b = C + m;
77  }
78  else if (H < 5.0f / 6) {
79  out.r = X + m; out.g = m; out.b = C + m;
80  }
81  else {
82  out.r = C + m; out.g = m; out.b = X + m;
83  }
84  return out;
85  }
86 
87  inline hsva_t() : h(0), s(0), v(0) {}
88 
89  inline hsva_t(const color3i& _) : hsva_t(_.r, _.g, _.b) {}
90  inline hsva_t(const color4i& _) : hsva_t(_.r, _.g, _.b) {}
91  inline hsva_t(const color3f& _) : hsva_t((float)_.r / 255, (float)_.g / 255, (float)_.b / 255) {}
92  inline hsva_t(const color4f& _) : hsva_t((float)_.r / 255, (float)_.g / 255, (float)_.b / 255) {}
93 
94  inline hsva_t(const pixfloat1& _) {
95  h = s = 0;
96  v = _.x;
97  }
98 
99  inline hsva_t(const pixfloat3& _) : hsva_t(_.r, _.g, _.b) {}
100 
101  inline hsva_t(const pixfloat4& _) : hsva_t(_.r, _.g, _.b) {}
102 
103  inline hsva_t(const pixint1& _) {
104  h = s = 0;
105  v = (float)_.x / 255;
106  }
107 
108  inline hsva_t(const pixint3& _) : hsva_t((float)_.r / 255, (float)_.g / 255, (float)_.b / 255) {}
109 
110  inline hsva_t(const pixint4& _) : hsva_t((float)_.r / 255, (float)_.g / 255, (float)_.b / 255) {}
111 
112  };
113 
114 }
115 }
float pixfloat
Definition: basic_types.h:35
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
HSVA quad (for hue, saturation, value and alpha)
Definition: color_spaces.h:34
hsva_t(const pixfloat3 &_)
Definition: color_spaces.h:99
hsva_t(const color3f &_)
Definition: color_spaces.h:91
hsva_t(const color4f &_)
Definition: color_spaces.h:92
hsva_t(pixfloat r, pixfloat g, pixfloat b)
Constructs an HSVA quad from r, g, b, a values.
Definition: color_spaces.h:40
hsva_t(const pixint3 &_)
Definition: color_spaces.h:108
hsva_t(const color4i &_)
Definition: color_spaces.h:90
hsva_t(const pixint4 &_)
Definition: color_spaces.h:110
hsva_t(const pixfloat4 &_)
Definition: color_spaces.h:101
hsva_t(const pixint1 &_)
Definition: color_spaces.h:103
hsva_t(const color3i &_)
Definition: color_spaces.h:89
hsva_t(const pixfloat1 &_)
Definition: color_spaces.h:94
Monochromatic floating point arithmetic.
Trichromatic floating point arithmetic.
4-channel floating point arithmetic
Monochromatic integer arithmetic.
Trichromatic integer arithmetic.
4-channel integer arithmetic
float modf(float x, float y)
Definition: utils.hpp:32
jobject jlong jint jint jint jint g
jobject jlong jint jint jint jint jint b
jobject jlong jint jint jint r
JNIEnv jlong jint out