diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2012-07-01 22:59:22 +0200 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2012-07-01 22:59:22 +0200 |
commit | d9c5a439d09211ec210195709d275596c6c3c9ba (patch) | |
tree | b536721f8ac5f42649345eeb836259bbf6a887e3 /src/graphics/common/color.cpp | |
parent | 9bd4ec03b272e7925b11c3efc2bd8460894ea589 (diff) | |
download | colobot-d9c5a439d09211ec210195709d275596c6c3c9ba.tar.gz colobot-d9c5a439d09211ec210195709d275596c6c3c9ba.tar.bz2 colobot-d9c5a439d09211ec210195709d275596c6c3c9ba.zip |
CGLDevice implementation
- extended Gfx::CDevice interface
- written OpenGL implementation in Gfx::CGLDevice
- rewrote color and light module
- added Gfx::VertexCol
- added array casts to Math::Vector, Math::Matrix and Gfx::Color
Diffstat (limited to 'src/graphics/common/color.cpp')
-rw-r--r-- | src/graphics/common/color.cpp | 173 |
1 files changed, 63 insertions, 110 deletions
diff --git a/src/graphics/common/color.cpp b/src/graphics/common/color.cpp index dd502f9..c1b7337 100644 --- a/src/graphics/common/color.cpp +++ b/src/graphics/common/color.cpp @@ -21,130 +21,83 @@ #include "math/func.h" -// Returns the color corresponding long. - -long Gfx::RetColor(float intensity) -{ - long color; - - if ( intensity <= 0.0f ) return 0x00000000; - if ( intensity >= 1.0f ) return 0xffffffff; - - color = (int)(intensity*255.0f)<<24; - color |= (int)(intensity*255.0f)<<16; - color |= (int)(intensity*255.0f)<<8; - color |= (int)(intensity*255.0f); - - return color; -} - -// Returns the color corresponding long. - -long Gfx::RetColor(Gfx::Color intensity) +Gfx::ColorHSV Gfx::RGB2HSV(Gfx::Color color) { - long color; - - color = (int)(intensity.a*255.0f)<<24; - color |= (int)(intensity.r*255.0f)<<16; - color |= (int)(intensity.g*255.0f)<<8; - color |= (int)(intensity.b*255.0f); + Gfx::ColorHSV result; - return color; -} + float min = Math::Min(color.r, color.g, color.b); + float max = Math::Max(color.r, color.g, color.b); -// Returns the color corresponding Color. + result.v = max; // intensity -Gfx::Color Gfx::RetColor(long intensity) -{ - Gfx::Color color; - - color.r = (float)((intensity>>16)&0xff)/256.0f; - color.g = (float)((intensity>>8 )&0xff)/256.0f; - color.b = (float)((intensity>>0 )&0xff)/256.0f; - color.a = (float)((intensity>>24)&0xff)/256.0f; + if ( max == 0.0f ) + { + result.s = 0.0f; // saturation + result.h = 0.0f; // undefined color! + } + else + { + float delta = max-min; + result.s = delta/max; // saturation + + if ( color.r == max ) // between yellow & magenta + { + result.h = (color.g-color.b)/delta; + } + else if ( color.g == max ) // between cyan & yellow + { + result.h = 2.0f+(color.b-color.r)/delta; + } + else // between magenta & cyan + { + result.h = 4.0f+(color.r-color.g)/delta; + } + + result.h *= 60.0f; // in degrees + if ( result.h < 0.0f ) result.h += 360.0f; + result.h /= 360.0f; // 0..1 + } - return color; + return result; } - -// RGB to HSV conversion. - -void Gfx::RGB2HSV(Gfx::Color src, Gfx::ColorHSV &dest) +Gfx::Color Gfx::HSV2RGB(Gfx::ColorHSV color) { - float min, max, delta; + Gfx::Color result; - min = Math::Min(src.r, src.g, src.b); - max = Math::Max(src.r, src.g, src.b); + color.h = Math::Norm(color.h)*360.0f; + color.s = Math::Norm(color.s); + color.v = Math::Norm(color.v); - dest.v = max; // intensity - - if ( max == 0.0f ) - { - dest.s = 0.0f; // saturation - dest.h = 0.0f; // undefined color! - } - else - { - delta = max-min; - dest.s = delta/max; // saturation - - if ( src.r == max ) // between yellow & magenta - { - dest.h = (src.g-src.b)/delta; - } - else if ( src.g == max ) // between cyan & yellow + if ( color.s == 0.0f ) // zero saturation? { - dest.h = 2.0f+(src.b-src.r)/delta; + result.r = color.v; + result.g = color.v; + result.b = color.v; // gray } - else // between magenta & cyan + else { - dest.h = 4.0f+(src.r-src.g)/delta; + if ( color.h == 360.0f ) color.h = 0.0f; + color.h /= 60.0f; + int i = (int)color.h; // integer part (0 .. 5) + float f = color.h-i; // fractional part + + float v = color.v; + float p = color.v*(1.0f-color.s); + float q = color.v*(1.0f-(color.s*f)); + float t = color.v*(1.0f-(color.s*(1.0f-f))); + + switch (i) + { + case 0: result.r=v; result.g=t; result.b=p; break; + case 1: result.r=q; result.g=v; result.b=p; break; + case 2: result.r=p; result.g=v; result.b=t; break; + case 3: result.r=p; result.g=q; result.b=v; break; + case 4: result.r=t; result.g=p; result.b=v; break; + case 5: result.r=v; result.g=p; result.b=q; break; + } } - dest.h *= 60.0f; // in degrees - if ( dest.h < 0.0f ) dest.h += 360.0f; - dest.h /= 360.0f; // 0..1 - } -} - -// HSV to RGB conversion. - -void Gfx::HSV2RGB(Gfx::ColorHSV src, Gfx::Color &dest) -{ - int i; - float f,v,p,q,t; - - src.h = Math::Norm(src.h)*360.0f; - src.s = Math::Norm(src.s); - src.v = Math::Norm(src.v); - - if ( src.s == 0.0f ) // zero saturation? - { - dest.r = src.v; - dest.g = src.v; - dest.b = src.v; // gray - } - else - { - if ( src.h == 360.0f ) src.h = 0.0f; - src.h /= 60.0f; - i = (int)src.h; // integer part (0 .. 5) - f = src.h-i; // fractional part - - v = src.v; - p = src.v*(1.0f-src.s); - q = src.v*(1.0f-(src.s*f)); - t = src.v*(1.0f-(src.s*(1.0f-f))); - - switch (i) - { - case 0: dest.r=v; dest.g=t; dest.b=p; break; - case 1: dest.r=q; dest.g=v; dest.b=p; break; - case 2: dest.r=p; dest.g=v; dest.b=t; break; - case 3: dest.r=p; dest.g=q; dest.b=v; break; - case 4: dest.r=t; dest.g=p; dest.b=v; break; - case 5: dest.r=v; dest.g=p; dest.b=q; break; - } - } + return result; } |