Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

sccolor.cpp

Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008                           sccolor.cpp  -  description
00009                              -------------------
00010     begin                : Sun Sep 9 2001
00011     copyright            : (C) 2001 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include <assert.h>
00025 
00026 #include "commonstrings.h"
00027 #include "sccolor.h"
00028 #include "sccolorengine.h"
00029 #include "scconfig.h"
00030 #include "scribuscore.h"
00031 #include "scribusdoc.h"
00032 
00033 ScColor::ScColor(void)
00034 {
00035 //  Model = colorModelCMYK;
00036 //  CR = MG = YB = K = 0;
00037     Model = colorModelRGB;
00038     CR = 150;
00039     MG = 100;
00040     YB = 50;
00041     K = 0;
00042     Spot = false;
00043     Regist = false;
00044 }
00045 
00046 ScColor::ScColor(int c, int m, int y, int k)
00047 {
00048     Spot = false;
00049     Regist = false;
00050     setColor(c, m, y, k);
00051 }
00052  
00053 ScColor::ScColor(int r, int g, int b)
00054 {
00055     Spot = false;
00056     Regist = false;
00057     setColorRGB(r, g, b);
00058 }
00059 
00060 bool ScColor::operator==(const ScColor& other) const
00061 {
00062     if (Model != other.Model)
00063         return false;
00064     if (Spot != other.Spot)
00065         return false;
00066     if (Regist != other.Regist)
00067         return false;
00068     if (Model == colorModelRGB)
00069     {
00070         return ((CR == other.CR) && (MG == other.MG) && (YB == other.YB));
00071     }
00072     if (Model == colorModelCMYK)
00073     {
00074         return ((CR == other.CR) && (MG == other.MG) && (YB == other.YB) && (K == other.K));
00075     }
00076     return false;
00077 }
00078 
00079 void ScColor::setColor(int c, int m, int y, int k)
00080 {
00081     CR = c;
00082     MG = m;
00083     YB = y;
00084     K = k;
00085     Model = colorModelCMYK;
00086 }
00087 
00088 void ScColor::setColorRGB(int r, int g, int b)
00089 {
00090     CR = r;
00091     MG = g;
00092     YB = b;
00093     Model = colorModelRGB;
00094 }
00095 
00096 colorModel ScColor::getColorModel () const
00097 {
00098     return Model;
00099 }
00100 
00101 void ScColor::fromQColor(QColor color)
00102 {
00103     int r, g, b;
00104     color.getRgb(&r, &g, &b);
00105     setColorRGB(r, g, b);
00106 }
00107 
00108 void ScColor::getRawRGBColor(int *r, int *g, int *b) const
00109 {
00110     if (Model == colorModelRGB)
00111     {
00112         *r = CR;
00113         *g = MG;
00114         *b = YB;
00115     }
00116     else
00117     {
00118         *r = 255-qMin(255, CR + K);
00119         *g = 255-qMin(255, MG + K);
00120         *b = 255-qMin(255, YB + K);
00121     }
00122 }
00123 
00124 QColor ScColor::getRawRGBColor() const
00125 {
00126     if (Model == colorModelRGB)
00127         return QColor(CR, MG, YB);
00128     return QColor(255-qMin(255, CR + K), 255-qMin(255, MG + K), 255 - qMin(255, YB + K));
00129 }
00130 
00131 void ScColor::getRGB(int *r, int *g, int *b) const
00132 {
00133     if( Model != colorModelRGB )
00134         qDebug("calling getRGB with a cmyk color");
00135     *r = CR;
00136     *g = MG;
00137     *b = YB;
00138 }
00139 
00140 void ScColor::getCMYK(int *c, int *m, int *y, int *k) const
00141 {
00142     if( Model != colorModelCMYK )
00143         qDebug("calling getCMYK with a rgb color");
00144     *c = CR;
00145     *m = MG;
00146     *y = YB;
00147     *k = K;
00148 }
00149 
00150 QString ScColor::name()
00151 {
00152     QString tmp, name="#";
00153     switch (Model) 
00154     {
00155     case colorModelCMYK:
00156         tmp.setNum(CR, 16);
00157         if (tmp.length() < 2)
00158             tmp.insert(0, "0");
00159         name += tmp;
00160         tmp.setNum(MG, 16);
00161         if (tmp.length() < 2)
00162             tmp.insert(0, "0");
00163         name += tmp;
00164         tmp.setNum(YB, 16);
00165         if (tmp.length() < 2)
00166             tmp.insert(0, "0");
00167         name += tmp;
00168         tmp.setNum(K, 16);
00169         if (tmp.length() < 2)
00170             tmp.insert(0, "0");
00171         name += tmp;
00172         break;
00173     case colorModelRGB:
00174         tmp.setNum(CR, 16);
00175         if (tmp.length() < 2)
00176             tmp.insert(0, "0");
00177         name += tmp;
00178         tmp.setNum(MG, 16);
00179         if (tmp.length() < 2)
00180             tmp.insert(0, "0");
00181         name += tmp;
00182         tmp.setNum(YB, 16);
00183         if (tmp.length() < 2)
00184             tmp.insert(0, "0");
00185         name += tmp;
00186         break;
00187     default:
00188         name = CommonStrings::None;
00189     }
00190     return name;
00191 }
00192 
00193 QString ScColor::nameCMYK(const ScribusDoc* doc)
00194 {
00195     if ((Model != colorModelCMYK) && (!doc))
00196         qDebug("calling nameCMYK with a rgb color");
00197     CMYKColor cmyk;
00198     int c, m, y, k;
00199     QString tmp, name = CommonStrings::None;
00200     ScColorEngine::getCMYKValues(*this, doc, cmyk);
00201     cmyk.getValues(c, m, y, k);
00202     name="#";
00203     tmp.setNum(c, 16);
00204     if (tmp.length() < 2)
00205         tmp.insert(0, "0");
00206     name += tmp;
00207     tmp.setNum(m, 16);
00208     if (tmp.length() < 2)
00209         tmp.insert(0, "0");
00210     name += tmp;
00211     tmp.setNum(y, 16);
00212     if (tmp.length() < 2)
00213         tmp.insert(0, "0");
00214     name += tmp;
00215     tmp.setNum(k, 16);
00216     if (tmp.length() < 2)
00217         tmp.insert(0, "0");
00218     name += tmp;
00219     return name;
00220 }
00221 
00222 QString ScColor::nameRGB(const ScribusDoc* doc)
00223 {
00224     if ((Model != colorModelRGB) && (!doc))
00225         qDebug("calling nameRGB with a cmyk color");
00226     int r, g, b;
00227     RGBColor rgb;
00228     QString tmp, name = CommonStrings::None;
00229     ScColorEngine::getRGBValues(*this, doc, rgb);
00230     rgb.getValues(r, g, b);
00231     name="#";
00232     tmp.setNum(r, 16);
00233     if (tmp.length() < 2)
00234         tmp.insert(0, "0");
00235     name += tmp;
00236     tmp.setNum(g, 16);
00237     if (tmp.length() < 2)
00238         tmp.insert(0, "0");
00239     name += tmp;
00240     tmp.setNum(b, 16);
00241     if (tmp.length() < 2)
00242         tmp.insert(0, "0");
00243     name += tmp;
00244     return name;
00245 }
00246 
00247 void ScColor::setNamedColor(QString name)
00248 {
00249     bool ok;
00250     if (name.length () == 9)
00251     {
00252         int c = name.mid(1,2).toInt(&ok, 16);
00253         int m = name.mid(3,2).toInt(&ok, 16);
00254         int y = name.mid(5,2).toInt(&ok, 16);
00255         int k = name.mid(7,2).toInt(&ok, 16);
00256         setColor(c, m, y, k);
00257     }
00258     else if (name.length () == 7)
00259     {
00260         int r = name.mid(1,2).toInt(&ok, 16);
00261         int g = name.mid(3,2).toInt(&ok, 16);
00262         int b = name.mid(5,2).toInt(&ok, 16);
00263         setColorRGB(r, g, b);
00264     }
00265 }
00266 
00267 bool ScColor::isRegistrationColor() const
00268 {
00269     return Regist;
00270 }
00271 
00272 void ScColor::setRegistrationColor(bool s)
00273 {
00274     Regist = s;
00275 }
00276 
00277 bool ScColor::isSpotColor() const
00278 {
00279     return Spot;
00280 }
00281 
00282 void ScColor::setSpotColor(bool s)
00283 {
00284     Spot = s;
00285 }
00286 
00287 ColorList::ColorList(ScribusDoc* doc, bool retainDoc) : QMap<QString,ScColor>()
00288 {
00289     m_doc = doc;
00290     m_retainDoc = retainDoc;
00291 }
00292 
00293 void ColorList::setDocument(ScribusDoc* doc)
00294 {
00295     m_doc = doc;
00296 }
00297 
00298 ColorList& ColorList::operator= (const ColorList& list)
00299 {
00300     clear();
00301     if (!m_retainDoc)
00302         m_doc = list.m_doc;
00303     addColors(list);
00304     return *this;
00305 }
00306 
00307 void ColorList::addColors(const ColorList& colorList, bool overwrite)
00308 {
00309     ColorList::ConstIterator it;
00310     ColorList::ConstIterator itend;
00311     itend = colorList.end();
00312     for (it = colorList.begin(); it != itend; ++it)
00313     {
00314         if (overwrite || !contains(it.key()))
00315             insert(it.key(), it.value());
00316     }
00317 }
00318 
00319 void ColorList::copyColors(const ColorList& colorList, bool overwrite)
00320 {
00321     clear();
00322     addColors(colorList, overwrite);
00323 }
00324 
00325 ScribusDoc* ColorList::document(void)
00326 { 
00327     return m_doc;
00328 }
00329 
00330 void ColorList::ensureBlackAndWhite(void)
00331 {
00332     ensureBlack();
00333     ensureWhite();
00334 }
00335 
00336 void ColorList::ensureBlack(void)
00337 {
00338     bool addBlack = true;
00339     ColorList::Iterator itb = find("Black");
00340     if (itb != end())
00341     {
00342         ScColor& black = itb.value();
00343         colorModel model = black.getColorModel();
00344         if (model == colorModelCMYK)
00345         {
00346             int c, m, y, k;
00347             black.getCMYK(&c, &m, &y, &k);
00348             if (c == 0 && m == 0 && y == 0 && k == 255)
00349                 addBlack = false;
00350         }
00351     }
00352     if (addBlack)
00353         insert("Black", ScColor(0, 0, 0, 255));
00354 }
00355 
00356 void ColorList::ensureWhite(void)
00357 {
00358     bool addWhite = true;
00359     ColorList::Iterator itw = find("White");
00360     if (itw != end())
00361     {
00362         ScColor& white = itw.value();
00363         colorModel model = white.getColorModel();
00364         if (model == colorModelCMYK)
00365         {
00366             int c, m, y, k;
00367             white.getCMYK(&c, &m, &y, &k);
00368             if (c == 0 && m == 0 && y == 0 && k == 0)
00369                 addWhite = false;
00370         }
00371     }
00372     if (addWhite)
00373         insert("White", ScColor(0, 0, 0, 0));
00374 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.