00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Color.h"
00026
00027 #ifdef WIN32
00028 #include "win32/Color_Win32.cpp"
00029 #else
00030 #include "linux/Color_Linux.cpp"
00031 #endif
00032
00033
00034 namespace cmlabs {
00035
00036
00037
00038
00039
00040 Color::Color() {
00041 init(-1,-1,-1);
00042 }
00043
00044 Color::Color(int red, int green, int blue) {
00045 init(red, green, blue);
00046 }
00047
00048 Color::Color(XMLNode* node) {
00049 init(-1,-1,-1);
00050 fromXML(node);
00051 }
00052
00053 Color::Color(const Color &color) {
00054 init(color.r, color.g, color.b);
00055 size = color.size;
00056 }
00057
00058 Color::Color(JString colorName) {
00059 init(colorName);
00060 }
00061
00062 Object* Color::clone() const {
00063 Color* col = new Color(r, g, b);
00064 return (Object*) col;
00065 }
00066
00067 void Color::init(int red, int green, int blue) {
00068 pen = NULL;
00069 brush = NULL;
00070 r = red;
00071 g = green;
00072 b = blue;
00073 size = 1;
00074 }
00075
00076 void Color::init(JString colorName) {
00077 JString name = colorName.toLowerCase();
00078
00079 if (name.contains(",")) {
00080 Collection col = name.split(",");
00081 int red = col.get(0).trim().toInt();
00082 int green = col.get(1).trim().toInt();
00083 int blue = col.get(2).trim().toInt();
00084 init(red, green, blue);
00085 return;
00086 }
00087
00088 if (name.equalsIgnoreCase("red"))
00089 init(255, 0, 0);
00090 else if (name.equalsIgnoreCase("darkred"))
00091 init(125, 0, 0);
00092 else if (name.equalsIgnoreCase("lightred"))
00093 init(255, 125, 125);
00094 else if (name.equalsIgnoreCase("green"))
00095 init(0, 255, 0);
00096 else if (name.equalsIgnoreCase("darkgreen"))
00097 init(0, 125, 0);
00098 else if (name.equalsIgnoreCase("lightgreen"))
00099 init(125, 255, 125);
00100 else if (name.equalsIgnoreCase("blue"))
00101 init(0, 0, 255);
00102 else if (name.equalsIgnoreCase("darkblue"))
00103 init(0, 0, 125);
00104 else if (name.equalsIgnoreCase("lightblue"))
00105 init(125, 125, 255);
00106 else if (name.equalsIgnoreCase("white"))
00107 init(255, 255, 255);
00108 else if (name.equalsIgnoreCase("black"))
00109 init(0, 0, 0);
00110 else if (name.equalsIgnoreCase("gray"))
00111 init(125, 125, 125);
00112 else if (name.equalsIgnoreCase("none"))
00113 init(-1, -1, -1);
00114 }
00115
00116 bool Color::setNoColor() {
00117 r = g = b = -1;
00118 return true;
00119 }
00120
00121 bool Color::isValid() {
00122 return (!isNoColor());
00123 }
00124
00125 bool Color::isNoColor() {
00126 return ( (r < 0) || (g < 0) || (b < 0) );
00127 }
00128
00129 Color Color::mixOn(Color& color, double weight) {
00130
00131 if (weight > 1)
00132 weight = 1;
00133 if (weight < 0)
00134 weight = 0;
00135 Color resColor;
00136 resColor.r = (int) (color.r*(1-weight) + this->r*weight);
00137 resColor.g = (int) (color.g*(1-weight) + this->g*weight);
00138 resColor.b = (int) (color.b*(1-weight) + this->b*weight);
00139 return resColor;
00140 }
00141
00142 int Color::getGrayValue() {
00143 return (int)(((double)r+g+b)/3.0);
00144 }
00145
00146 Color Color::getReverseColor() {
00147 Color resColor;
00148 resColor.r = 255-r;
00149 resColor.g = 255-g;
00150 resColor.b = 255-b;
00151 return resColor;
00152 }
00153
00154 bool Color::fromXML(XMLNode* node) {
00155 if (node == NULL)
00156 return false;
00157 if (node->hasAttribute("name"))
00158 this->init(node->getAttribute("name"));
00159 else {
00160 r = node->getAttribute("red").toInt();
00161 g = node->getAttribute("green").toInt();
00162 b = node->getAttribute("blue").toInt();
00163 }
00164 return true;
00165 }
00166
00167 JString Color::toXML() {
00168 return toXML("color");
00169 }
00170
00171 JString Color::toXML(JString name) {
00172 return JString::format("<%s red=\"%d\" green=\"%d\" blue=\"%d\" />", (char*) name.xmlStringEncode(), r, g, b);
00173 }
00174
00175 double Color::distance(Color &otherColor) {
00176 return pow((double)pow((double)r-otherColor.r, 2) + pow((double)g-otherColor.g, 2) + pow((double)b-otherColor.b, 2) , (1.0/3));
00177 }
00178
00179 ObjectCollection* Color::createColorsMaxDifference(int count, bool mix) {
00180
00181 int rcount = (int)(count / 3);
00182 if (rcount <= 0) rcount = 1;
00183 int gcount = (int)(count / 3);
00184 if (gcount <= 0) gcount = 1;
00185 int bcount = count - rcount - gcount;
00186 if (bcount <= 0) bcount = 1;
00187
00188 int n;
00189 ObjectCollection* col = new ObjectCollection();
00190 int value = 0;
00191 for (n=0; n<rcount; n++) {
00192 value = (int)((n+1)*(255.0/rcount));
00193 if (value >= 256) value = 255;
00194 col->add(new Color(value, 0, 0));
00195 }
00196 for (n=0; n<gcount; n++) {
00197 value = (int)((n+1)*(255.0/gcount));
00198 if (value >= 256) value = 255;
00199 col->add(new Color(0, value, 0));
00200 }
00201 for (n=0; n<bcount; n++) {
00202 value = (int)((n+1)*(255.0/bcount));
00203 if (value >= 256) value = 255;
00204 col->add(new Color(0, 0, value));
00205 }
00206
00207 if (mix) col->sortRandomMix();
00208 return col;
00209 }
00210
00211
00212
00213
00214
00215
00216 }