sdlcolor.h

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #ifndef WT_SDLCOLOR_H
00022 #define WT_SDLCOLOR_H
00023 
00024 #include <iosfwd>
00025 #include <cassert>
00026 
00027 #include <SDL/SDL_video.h>
00028 
00029 namespace Wt {
00030 
00031 /*!
00032 \note don't change object size to keep SDL_Color compatibility.
00033 that means:
00034  - no new data members
00035  - no virtual functions
00036 */
00037 class SDLColor {
00038 public:
00039     /// Default constructor (opaque black)
00040     SDLColor() {
00041         assert(sizeof(SDLColor) == sizeof(SDL_Color));
00042         setColor(0, 0, 0, Opaque);
00043     }
00044 
00045     /// Construct from a triple/quadruple. (R, G, B, A)
00046     SDLColor(int red, int green, int blue, int alpha = Opaque) {
00047         assert(sizeof(SDLColor) == sizeof(SDL_Color));
00048         setColor(red, green, blue, alpha);
00049     }
00050 
00051     /// constructor from a base object
00052     SDLColor(const SDL_Color& color)
00053             : sdl_color(color) {
00054         assert(sizeof(SDLColor) == sizeof(SDL_Color));
00055     }
00056 
00057     /// assign operator
00058     SDLColor& operator=(const SDL_Color& color) {
00059         sdl_color = color;
00060         return *this;
00061     }
00062 
00063     operator const SDL_Color& () const {
00064         return sdl_color;
00065     }
00066 
00067     operator SDL_Color& ()  {
00068         return sdl_color;
00069     }
00070 
00071     int red() const {
00072         return sdl_color.r;
00073     }
00074 
00075     int green() const {
00076         return sdl_color.g;
00077     }
00078 
00079     int blue() const {
00080         return sdl_color.b;
00081     }
00082 
00083     int alpha() const {
00084         return sdl_color.unused;
00085     }
00086 
00087     void setRed(int red) {
00088         sdl_color.r = red;
00089     }
00090 
00091     void setGreen(int green) {
00092         sdl_color.g = green;
00093     }
00094 
00095     void setBlue(int blue) {
00096         sdl_color.b = blue;
00097     }
00098 
00099     void setAlpha(int alpha) {
00100         sdl_color.unused = alpha;
00101     }
00102 
00103     void setColor(int red, int green, int blue, int alpha) {
00104         setRed(red);
00105         setGreen(green);
00106         setBlue(blue);
00107         setAlpha(alpha);
00108     }
00109 
00110     /// The alpha channel values for transparent and opaque
00111     enum {
00112         Transparent = SDL_ALPHA_TRANSPARENT,
00113         Opaque = SDL_ALPHA_OPAQUE
00114     };
00115 
00116     /// Compares colors (without alpha channel !)
00117     bool equals(const SDLColor &other) const {
00118         return sdl_color.r == other.sdl_color.r &&
00119                sdl_color.g == other.sdl_color.g &&
00120                sdl_color.b == other.sdl_color.b;
00121     }
00122 
00123     bool operator==(const SDLColor &other) const {
00124         return (memcmp(&sdl_color, &other.sdl_color, sizeof(SDL_Color)) == 0);
00125     }
00126 
00127     /// Inequality comparison
00128     bool operator!=(const SDLColor &other) const {
00129         return !(*this == other);
00130     }
00131 
00132     static void calcBlendFactors(int factor, int& new_factor, int& old_factor) {
00133         // it won't work in previous SDL
00134         assert(Opaque);
00135         // transparent == 0
00136         new_factor = factor;
00137         old_factor = Opaque - new_factor;
00138     }
00139 
00140     static SDLColor blend(const SDLColor& old_color, int old_factor,
00141                           const SDLColor& new_color, int new_factor,
00142                           int new_alpha_factor = Transparent, int old_alpha_factor = Opaque) {
00143 
00144         return SDLColor((new_factor * new_color.red() + old_factor * old_color.red()) / 255,
00145                         (new_factor * new_color.green() + old_factor * old_color.green()) / 255,
00146                         (new_factor * new_color.blue() + old_factor * old_color.blue()) / 255,
00147                         (new_alpha_factor * new_color.alpha() + old_alpha_factor * old_color.alpha()) / 255);
00148     }
00149 
00150 private:
00151     SDL_Color sdl_color;
00152 };
00153 
00154 } // namespace
00155 
00156 #endif //WT_SDLCOLOR_H

Generated Fri Jul 28 19:23:00 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.