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