/src/msaColor.h

http://github.com/mazbox/Speakotron · C Header · 265 lines · 173 code · 43 blank · 49 comment · 47 complexity · f0264f3c901b619b9aaedfcadca42b72 MD5 · raw file

  1. /*
  2. * msaColor.h
  3. * opencvExample
  4. *
  5. * Created by Marek Bereza on 05/09/2010.
  6. * Copyright 2010 Marek Bereza. All rights reserved.
  7. *
  8. */
  9. /***********************************************************************
  10. Copyright (c) 2008, 2009, Memo Akten, www.memo.tv
  11. *** The Mega Super Awesome Visuals Company ***
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * Neither the name of MSA Visuals nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  34. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. * OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * ***********************************************************************/
  38. #pragma once
  39. #include "ofMain.h"
  40. class msaColor : public ofColor{
  41. public:
  42. void set(float r, float g, float b, float a = 1) {
  43. this->r = r;
  44. this->g = g;
  45. this->b = b;
  46. this->a = a;
  47. }
  48. void setGL() {
  49. glColor4f(r, g, b, a);
  50. }
  51. void setClamp(float r, float g, float b, float a = 1) {
  52. set(r, g, b, a);
  53. clamp();
  54. }
  55. void clamp() {
  56. if(r > 1) r = 1;
  57. if(g > 1) g = 1;
  58. if(b > 1) b = 1;
  59. if(a > 1) a = 1;
  60. }
  61. msaColor lerpTo(const msaColor& target, float t ) {
  62. return (*this * t) + (target * (1-t));
  63. }
  64. float getLuminance() {
  65. return (0.2126*r) + (0.7152*g) + (0.0722*b);
  66. }
  67. // H [0..360], S and V [0..1]
  68. void setHSV(float h, float s, float v, float a = 1) {
  69. h = int(h) % 360;
  70. int i = (int)floor(h/60.0f) % 6;
  71. float f = h/60.0f - floor(h/60.0f);
  72. float p = v * (1 - s);
  73. float q = v * (1 - s * f);
  74. float t = v * (1 - (1 - f) * s);
  75. switch (i) {
  76. case 0: set(v, t, p, a); break;
  77. case 1: set(q, v, p, a); break;
  78. case 2: set(p, v, t, a); break;
  79. case 3: set(p, q, v, a); break;
  80. case 4: set(t, p, v, a); break;
  81. case 5: set(v, p, q, a); break;
  82. }
  83. }
  84. // assumes RGB is normalized [0..1]
  85. // returns H [0..360], S and V [0..1]
  86. void getHSV(ofPoint& outHSV) {
  87. float h, s, v;
  88. float y, r1,g1,b1;
  89. v = r;
  90. if (v<g) v=g;
  91. if (v<b) v=b;
  92. y = r;
  93. if (y>g) y=g;
  94. if (y>b) y=b;
  95. if (v != 0) s = (v-y)/v;
  96. else s = 0;
  97. if (s == 0) {
  98. h = 0;
  99. s = 0;
  100. v = (int)(v*100);
  101. }
  102. r1 = (v-r)/(v-y);
  103. g1 = (v-g)/(v-y);
  104. b1 = (v-b)/(v-y);
  105. if (r == v){
  106. if (g == y) h = 5.+b1;
  107. else h = 1.-g1;
  108. }
  109. else if (g == v){
  110. if (b == y) h = r1+1.;
  111. else h = 3.-b1;
  112. }
  113. else{
  114. if (r == y) h = 3.+g1;
  115. else h = 5.-r1;
  116. }
  117. // convert it all
  118. h = h * 60.;
  119. if (h >= 360.) h = h-360.;
  120. outHSV.set(h, s, v);
  121. }
  122. msaColor( float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f ) {
  123. set(r, g, b, a);
  124. }
  125. msaColor( const msaColor & col){
  126. set(col.r, col.g, col.b, col.a);
  127. }
  128. //equalitg
  129. bool operator==( const msaColor& col ) {
  130. return (r == col.r) && (g == col.g) && (b == col.b) && (a == col.a);
  131. }
  132. //inequalitg
  133. bool operator!=( const msaColor& col ) {
  134. return (r != col.r) || (g != col.g) || (b != col.b) || (a != col.a);
  135. }
  136. //Set
  137. msaColor & operator=( const msaColor& col ){
  138. set(col.r, col.g, col.b, col.a);
  139. return *this;
  140. }
  141. // Add
  142. msaColor operator+( const msaColor& col ) const {
  143. return msaColor( r+col.r, g+col.g, b+col.b, a+col.a );
  144. }
  145. msaColor & operator+=( const msaColor& col ) {
  146. r += col.r;
  147. g += col.g;
  148. b += col.b;
  149. a += col.a;
  150. return *this;
  151. }
  152. msaColor & operator+=( const float & val ) {
  153. r += val;
  154. g += val;
  155. b += val;
  156. a += val;
  157. return *this;
  158. }
  159. // Subtract
  160. msaColor operator-(const msaColor& col) const {
  161. return msaColor( r-col.r, g-col.g, b-col.b, a-col.a);
  162. }
  163. msaColor & operator-=( const msaColor& col ) {
  164. r -= col.r;
  165. g -= col.g;
  166. b -= col.b;
  167. a -= col.a;
  168. return *this;
  169. }
  170. msaColor & operator-=( const float & val ) {
  171. r -= val;
  172. g -= val;
  173. b -= val;
  174. a -= val;
  175. return *this;
  176. }
  177. // Multiply
  178. msaColor operator*(const float& val) const {
  179. return msaColor( r*val, g*val, b*val, a*val);
  180. }
  181. msaColor & operator*=( const msaColor& col ) {
  182. r*=col.r;
  183. g*=col.g;
  184. b*=col.b;
  185. a*=col.a;
  186. return *this;
  187. }
  188. msaColor & operator*=( const float & val ) {
  189. r*=val;
  190. g*=val;
  191. b*=val;
  192. a*=val;
  193. return *this;
  194. }
  195. // Divide
  196. msaColor operator/( const msaColor& col ) const {
  197. return msaColor( col.r!=0 ? r/col.r : r , col.g!=0 ? g/col.g : g, col.b!=0 ? b/col.b : b, col.a!=0 ? a/col.a : a);
  198. }
  199. msaColor operator/( const float &val ) const {
  200. if( val != 0){
  201. return msaColor( r/val, g/val, b/val, a/val );
  202. }
  203. return msaColor(r, g, b, a);
  204. }
  205. msaColor& operator/=( const msaColor& col ) {
  206. col.r!=0 ? r/=col.r : r;
  207. col.g!=0 ? g/=col.g : g;
  208. col.b!=0 ? b/=col.b : b;
  209. col.a!=0 ? a/=col.a : a;
  210. return *this;
  211. }
  212. msaColor& operator/=( const float &val ) {
  213. if( val != 0 ){
  214. r /= val;
  215. g /= val;
  216. b /= val;
  217. a /= val;
  218. }
  219. return *this;
  220. }
  221. };