Eneboo - Documentación para desarrolladores
|
00001 /* 00002 odf-gen: Simple API to generate OpenDocument documents. 00003 Copyright (C) 2009 Pablo Jorge, FuDePAN 00004 00005 This file is part of the odf-gen project. 00006 00007 odf-gen is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation, either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 odf-gen is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with odf-gen. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #ifndef COLOR_H 00022 #define COLOR_H 00023 00024 #include <time.h> 00025 00026 #include <iostream> 00027 #include <iomanip> 00028 #include <cstdlib> 00029 00030 class Color 00031 { 00032 public: 00033 Color() 00034 : _red( 0 ), 00035 _green( 0 ), 00036 _blue( 0 ), 00037 _valid( false ) 00038 {} 00039 00040 Color( unsigned char red, 00041 unsigned char green, 00042 unsigned char blue ) 00043 : _red( red ), 00044 _green( green ), 00045 _blue( blue ), 00046 _valid( true ) 00047 {} 00048 00049 Color( unsigned int color ) 00050 : _red( color >> 16 ), 00051 _green( color >> 8 ), 00052 _blue( color ), 00053 _valid( true ) 00054 {} 00055 00056 bool isValid() const 00057 { 00058 return _valid; 00059 } 00060 00061 Color& operator = ( const Color &other ) 00062 { 00063 _red = other._red; 00064 _green = other._green; 00065 _blue = other._blue; 00066 _valid = other._valid; 00067 return *this; 00068 } 00069 00070 std::ostream& operator << ( std::ostream& ostream ) const 00071 { 00072 ostream << "#" 00073 << std::hex 00074 << std::setfill('0') 00075 << std::setw(2) << static_cast< int >( _red ) 00076 << std::setw(2) << static_cast< int >( _green ) 00077 << std::setw(2) << static_cast< int >( _blue ) 00078 << std::dec; 00079 return ostream; 00080 } 00081 00082 private: 00083 unsigned char _red, 00084 _green, 00085 _blue; 00086 bool _valid; 00087 }; 00088 00089 inline 00090 std::ostream& operator << ( std::ostream &ostream, const Color& color ) 00091 { 00092 return color.operator << (ostream); 00093 } 00094 00095 class ColorGenerator 00096 { 00097 public: 00098 ColorGenerator() 00099 : _idx( 0 ) 00100 { 00101 srand( time( NULL ) ); 00102 } 00103 00104 Color next() 00105 { 00106 static unsigned int palette[] = { 0x0000ff, 0xff0000, 0x00ff00, 00107 0xff00ff, 0xffff00, 0x00ffff }; 00108 00109 if( _idx < sizeof(palette) / sizeof(palette[0]) ) 00110 return Color( palette[_idx++] ); 00111 00112 return Color( rand() ); 00113 } 00114 00115 private: 00116 uint _idx; 00117 }; 00118 00119 #endif // COLOR_H