summaryrefslogtreecommitdiffstats
path: root/core/color.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/color.cpp')
-rw-r--r--core/color.cpp291
1 files changed, 140 insertions, 151 deletions
diff --git a/core/color.cpp b/core/color.cpp
index 80a98da252..f052ddea8a 100644
--- a/core/color.cpp
+++ b/core/color.cpp
@@ -28,82 +28,80 @@
/*************************************************************************/
#include "color.h"
+#include "color_names.inc"
+#include "map.h"
#include "math_funcs.h"
#include "print_string.h"
-#include "map.h"
-#include "color_names.inc"
uint32_t Color::to_ARGB32() const {
- uint32_t c=(uint8_t)(a*255);
- c<<=8;
- c|=(uint8_t)(r*255);
- c<<=8;
- c|=(uint8_t)(g*255);
- c<<=8;
- c|=(uint8_t)(b*255);
+ uint32_t c = (uint8_t)(a * 255);
+ c <<= 8;
+ c |= (uint8_t)(r * 255);
+ c <<= 8;
+ c |= (uint8_t)(g * 255);
+ c <<= 8;
+ c |= (uint8_t)(b * 255);
return c;
}
uint32_t Color::to_32() const {
- uint32_t c=(uint8_t)(a*255);
- c<<=8;
- c|=(uint8_t)(r*255);
- c<<=8;
- c|=(uint8_t)(g*255);
- c<<=8;
- c|=(uint8_t)(b*255);
+ uint32_t c = (uint8_t)(a * 255);
+ c <<= 8;
+ c |= (uint8_t)(r * 255);
+ c <<= 8;
+ c |= (uint8_t)(g * 255);
+ c <<= 8;
+ c |= (uint8_t)(b * 255);
return c;
}
float Color::get_h() const {
- float min = MIN( r, g );
- min = MIN( min, b );
- float max = MAX( r, g );
- max = MAX( max, b );
+ float min = MIN(r, g);
+ min = MIN(min, b);
+ float max = MAX(r, g);
+ max = MAX(max, b);
float delta = max - min;
- if( delta == 0 )
+ if (delta == 0)
return 0;
float h;
- if( r == max )
- h = ( g - b ) / delta; // between yellow & magenta
- else if( g == max )
- h = 2 + ( b - r ) / delta; // between cyan & yellow
+ if (r == max)
+ h = (g - b) / delta; // between yellow & magenta
+ else if (g == max)
+ h = 2 + (b - r) / delta; // between cyan & yellow
else
- h = 4 + ( r - g ) / delta; // between magenta & cyan
+ h = 4 + (r - g) / delta; // between magenta & cyan
- h/=6.0;
- if (h<0)
- h+=1.0;
+ h /= 6.0;
+ if (h < 0)
+ h += 1.0;
return h;
}
float Color::get_s() const {
-
- float min = MIN( r, g );
- min = MIN( min, b );
- float max = MAX( r, g );
- max = MAX( max, b );
+ float min = MIN(r, g);
+ min = MIN(min, b);
+ float max = MAX(r, g);
+ max = MAX(max, b);
float delta = max - min;
- return (max!=0) ? (delta / max) : 0;
-
+ return (max != 0) ? (delta / max) : 0;
}
float Color::get_v() const {
- float max = MAX( r, g );
- max = MAX( max, b );
+ float max = MAX(r, g);
+ max = MAX(max, b);
return max;
}
@@ -111,24 +109,24 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
int i;
float f, p, q, t;
- a=p_alpha;
+ a = p_alpha;
- if( p_s == 0 ) {
+ if (p_s == 0) {
// acp_hromatic (grey)
r = g = b = p_v;
return;
}
- p_h *=6.0;
- p_h = Math::fmod(p_h,6);
- i = Math::floor( p_h );
+ p_h *= 6.0;
+ p_h = Math::fmod(p_h, 6);
+ i = Math::floor(p_h);
f = p_h - i;
- p = p_v * ( 1 - p_s );
- q = p_v * ( 1 - p_s * f );
- t = p_v * ( 1 - p_s * ( 1 - f ) );
+ p = p_v * (1 - p_s);
+ q = p_v * (1 - p_s * f);
+ t = p_v * (1 - p_s * (1 - f));
- switch( i ) {
+ switch (i) {
case 0: // Red is the dominant color
r = p_v;
g = t;
@@ -164,170 +162,166 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
void Color::invert() {
- r=1.0-r;
- g=1.0-g;
- b=1.0-b;
+ r = 1.0 - r;
+ g = 1.0 - g;
+ b = 1.0 - b;
}
void Color::contrast() {
- r=Math::fmod(r+0.5,1.0);
- g=Math::fmod(g+0.5,1.0);
- b=Math::fmod(b+0.5,1.0);
+ r = Math::fmod(r + 0.5, 1.0);
+ g = Math::fmod(g + 0.5, 1.0);
+ b = Math::fmod(b + 0.5, 1.0);
}
Color Color::hex(uint32_t p_hex) {
- float a = (p_hex&0xFF)/255.0;
- p_hex>>=8;
- float b = (p_hex&0xFF)/255.0;
- p_hex>>=8;
- float g = (p_hex&0xFF)/255.0;
- p_hex>>=8;
- float r = (p_hex&0xFF)/255.0;
+ float a = (p_hex & 0xFF) / 255.0;
+ p_hex >>= 8;
+ float b = (p_hex & 0xFF) / 255.0;
+ p_hex >>= 8;
+ float g = (p_hex & 0xFF) / 255.0;
+ p_hex >>= 8;
+ float r = (p_hex & 0xFF) / 255.0;
- return Color(r,g,b,a);
+ return Color(r, g, b, a);
}
-static float _parse_col(const String& p_str, int p_ofs) {
+static float _parse_col(const String &p_str, int p_ofs) {
- int ig=0;
+ int ig = 0;
- for(int i=0;i<2;i++) {
+ for (int i = 0; i < 2; i++) {
- int c=p_str[i+p_ofs];
- int v=0;
+ int c = p_str[i + p_ofs];
+ int v = 0;
- if (c>='0' && c<='9') {
- v=c-'0';
- } else if (c>='a' && c<='f') {
- v=c-'a';
- v+=10;
- } else if (c>='A' && c<='F') {
- v=c-'A';
- v+=10;
+ if (c >= '0' && c <= '9') {
+ v = c - '0';
+ } else if (c >= 'a' && c <= 'f') {
+ v = c - 'a';
+ v += 10;
+ } else if (c >= 'A' && c <= 'F') {
+ v = c - 'A';
+ v += 10;
} else {
return -1;
}
- if (i==0)
- ig+=v*16;
+ if (i == 0)
+ ig += v * 16;
else
- ig+=v;
-
+ ig += v;
}
return ig;
-
}
Color Color::inverted() const {
- Color c=*this;
+ Color c = *this;
c.invert();
return c;
}
Color Color::contrasted() const {
- Color c=*this;
+ Color c = *this;
c.contrast();
return c;
}
-
-Color Color::html(const String& p_color) {
+Color Color::html(const String &p_color) {
String color = p_color;
- if (color.length()==0)
+ if (color.length() == 0)
return Color();
- if (color[0]=='#')
- color=color.substr(1,color.length()-1);
+ if (color[0] == '#')
+ color = color.substr(1, color.length() - 1);
- bool alpha=false;
+ bool alpha = false;
- if (color.length()==8) {
- alpha=true;
- } else if (color.length()==6) {
- alpha=false;
+ if (color.length() == 8) {
+ alpha = true;
+ } else if (color.length() == 6) {
+ alpha = false;
} else {
- ERR_EXPLAIN("Invalid Color Code: "+p_color);
+ ERR_EXPLAIN("Invalid Color Code: " + p_color);
ERR_FAIL_V(Color());
}
- int a=255;
+ int a = 255;
if (alpha) {
- a=_parse_col(color,0);
- if (a<0) {
- ERR_EXPLAIN("Invalid Color Code: "+p_color);
+ a = _parse_col(color, 0);
+ if (a < 0) {
+ ERR_EXPLAIN("Invalid Color Code: " + p_color);
ERR_FAIL_V(Color());
}
}
- int from=alpha?2:0;
+ int from = alpha ? 2 : 0;
- int r=_parse_col(color,from+0);
- if (r<0) {
- ERR_EXPLAIN("Invalid Color Code: "+p_color);
+ int r = _parse_col(color, from + 0);
+ if (r < 0) {
+ ERR_EXPLAIN("Invalid Color Code: " + p_color);
ERR_FAIL_V(Color());
}
- int g=_parse_col(color,from+2);
- if (g<0) {
- ERR_EXPLAIN("Invalid Color Code: "+p_color);
+ int g = _parse_col(color, from + 2);
+ if (g < 0) {
+ ERR_EXPLAIN("Invalid Color Code: " + p_color);
ERR_FAIL_V(Color());
}
- int b=_parse_col(color,from+4);
- if (b<0) {
- ERR_EXPLAIN("Invalid Color Code: "+p_color);
+ int b = _parse_col(color, from + 4);
+ if (b < 0) {
+ ERR_EXPLAIN("Invalid Color Code: " + p_color);
ERR_FAIL_V(Color());
}
- return Color(r/255.0,g/255.0,b/255.0,a/255.0);
+ return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
}
-bool Color::html_is_valid(const String& p_color) {
+bool Color::html_is_valid(const String &p_color) {
String color = p_color;
- if (color.length()==0)
+ if (color.length() == 0)
return false;
- if (color[0]=='#')
- color=color.substr(1,color.length()-1);
+ if (color[0] == '#')
+ color = color.substr(1, color.length() - 1);
- bool alpha=false;
+ bool alpha = false;
- if (color.length()==8) {
- alpha=true;
- } else if (color.length()==6) {
- alpha=false;
+ if (color.length() == 8) {
+ alpha = true;
+ } else if (color.length() == 6) {
+ alpha = false;
} else {
return false;
}
- int a=255;
+ int a = 255;
if (alpha) {
- a=_parse_col(color,0);
- if (a<0) {
+ a = _parse_col(color, 0);
+ if (a < 0) {
return false;
}
}
- int from=alpha?2:0;
+ int from = alpha ? 2 : 0;
- int r=_parse_col(color,from+0);
- if (r<0) {
+ int r = _parse_col(color, from + 0);
+ if (r < 0) {
return false;
}
- int g=_parse_col(color,from+2);
- if (g<0) {
+ int g = _parse_col(color, from + 2);
+ if (g < 0) {
return false;
}
- int b=_parse_col(color,from+4);
- if (b<0) {
+ int b = _parse_col(color, from + 4);
+ if (b < 0) {
return false;
}
return true;
-
}
Color Color::named(const String &p_name) {
@@ -340,12 +334,12 @@ Color Color::named(const String &p_name) {
name = name.replace("'", "");
name = name.replace(".", "");
name = name.to_lower();
-
- const Map<String, Color>::Element* color = _named_colors.find(name);
- if(color) {
+
+ const Map<String, Color>::Element *color = _named_colors.find(name);
+ if (color) {
return color->value();
} else {
- ERR_EXPLAIN("Invalid Color Name: "+p_name);
+ ERR_EXPLAIN("Invalid Color Name: " + p_name);
ERR_FAIL_V(Color());
}
}
@@ -353,48 +347,43 @@ Color Color::named(const String &p_name) {
String _to_hex(float p_val) {
int v = p_val * 255;
- v = CLAMP(v,0,255);
+ v = CLAMP(v, 0, 255);
String ret;
- for(int i=0;i<2;i++) {
+ for (int i = 0; i < 2; i++) {
- CharType c[2]={0,0};
- int lv = v&0xF;
- if (lv<10)
- c[0]='0'+lv;
+ CharType c[2] = { 0, 0 };
+ int lv = v & 0xF;
+ if (lv < 10)
+ c[0] = '0' + lv;
else
- c[0]='a'+lv-10;
+ c[0] = 'a' + lv - 10;
- v>>=4;
- String cs=(const CharType*)c;
+ v >>= 4;
+ String cs = (const CharType *)c;
ret = cs + ret;
}
return ret;
-
}
String Color::to_html(bool p_alpha) const {
String txt;
- txt+=_to_hex(r);
- txt+=_to_hex(g);
- txt+=_to_hex(b);
+ txt += _to_hex(r);
+ txt += _to_hex(g);
+ txt += _to_hex(b);
if (p_alpha)
- txt=_to_hex(a)+txt;
+ txt = _to_hex(a) + txt;
return txt;
-
}
-
float Color::gray() const {
- return (r+g+b)/3.0;
+ return (r + g + b) / 3.0;
}
Color::operator String() const {
- return rtos(r)+", "+rtos(g)+", "+rtos(b)+", "+rtos(a);
+ return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
}
-
-