Back to index

scribus-ng  1.3.4.dfsg+svn20071115
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 <qcolor.h>
00025 #include <qstring.h>
00026 #include <assert.h>
00027 
00028 #include "sccolor.h"
00029 #include "sccolorengine.h"
00030 #include "scribuscore.h"
00031 #include "scribusdoc.h"
00032 #include "commonstrings.h"
00033 #include "scconfig.h"
00034 
00035 
00036 ScColor::ScColor(void)
00037 {
00038 //     Model = colorModelCMYK;
00039 //     CR = MG = YB = K = 0;
00040        Model = colorModelRGB;
00041        CR = 150;
00042        MG = 100;
00043        YB = 50;
00044        K = 0;
00045        Spot = false;
00046        Regist = false;
00047 }
00048 
00049 ScColor::ScColor(int c, int m, int y, int k)
00050 {
00051        Spot = false;
00052        Regist = false;
00053        setColor(c, m, y, k);
00054 }
00055  
00056 ScColor::ScColor(int r, int g, int b)
00057 {
00058        Spot = false;
00059        Regist = false;
00060        setColorRGB(r, g, b);
00061 }
00062 
00063 bool ScColor::operator==(const ScColor& other) const
00064 {
00065        if (Model != other.Model)
00066               return false;
00067        if (Spot != other.Spot)
00068               return false;
00069        if (Regist != other.Regist)
00070               return false;
00071        if (Model == colorModelRGB)
00072        {
00073               return ((CR == other.CR) && (MG == other.MG) && (YB == other.YB));
00074        }
00075        if (Model == colorModelCMYK)
00076        {
00077               return ((CR == other.CR) && (MG == other.MG) && (YB == other.YB) && (K == other.K));
00078        }
00079        return false;
00080 }
00081 
00082 void ScColor::setColor(int c, int m, int y, int k)
00083 {
00084        CR = c;
00085        MG = m;
00086        YB = y;
00087        K = k;
00088        Model = colorModelCMYK;
00089 }
00090 
00091 void ScColor::setColorRGB(int r, int g, int b)
00092 {
00093        CR = r;
00094        MG = g;
00095        YB = b;
00096        Model = colorModelRGB;
00097 }
00098 
00099 colorModel ScColor::getColorModel () const
00100 {
00101        return Model;
00102 }
00103 
00104 void ScColor::fromQColor(QColor color)
00105 {
00106        int r, g, b;
00107        color.rgb(&r, &g, &b);
00108        setColorRGB(r, g, b);
00109 }
00110 
00111 void ScColor::getRawRGBColor(int *r, int *g, int *b) const
00112 {
00113        if (Model == colorModelRGB)
00114        {
00115               *r = CR;
00116               *g = MG;
00117               *b = YB;
00118        }
00119        else
00120        {
00121               *r = 255-QMIN(255, CR + K);
00122               *g = 255-QMIN(255, MG + K);
00123               *b = 255-QMIN(255, YB + K);
00124        }
00125 }
00126 
00127 QColor ScColor::getRawRGBColor() const
00128 {
00129        if (Model == colorModelRGB)
00130               return QColor(CR, MG, YB);
00131        return QColor(255-QMIN(255, CR + K), 255-QMIN(255, MG + K), 255 - QMIN(255, YB + K));
00132 }
00133 
00134 void ScColor::getRGB(int *r, int *g, int *b) const
00135 {
00136        if( Model != colorModelRGB )
00137               qDebug("calling getRGB with a cmyk color");
00138        *r = CR;
00139        *g = MG;
00140        *b = YB;
00141 }
00142 
00143 void ScColor::getCMYK(int *c, int *m, int *y, int *k) const
00144 {
00145        if( Model != colorModelCMYK )
00146               qDebug("calling getCMYK with a rgb color");
00147        *c = CR;
00148        *m = MG;
00149        *y = YB;
00150        *k = K;
00151 }
00152 
00153 QString ScColor::name()
00154 {
00155        QString tmp, name = CommonStrings::None;
00156        name="#";
00157        switch (Model) 
00158        {
00159        case colorModelCMYK:
00160               tmp.setNum(CR, 16);
00161               if (tmp.length() < 2)
00162                      tmp.insert(0, "0");
00163               name += tmp;
00164               tmp.setNum(MG, 16);
00165               if (tmp.length() < 2)
00166                      tmp.insert(0, "0");
00167               name += tmp;
00168               tmp.setNum(YB, 16);
00169               if (tmp.length() < 2)
00170                      tmp.insert(0, "0");
00171               name += tmp;
00172               tmp.setNum(K, 16);
00173               if (tmp.length() < 2)
00174                      tmp.insert(0, "0");
00175               name += tmp;
00176               break;
00177        case colorModelRGB:
00178               tmp.setNum(CR, 16);
00179               if (tmp.length() < 2)
00180                      tmp.insert(0, "0");
00181               name += tmp;
00182               tmp.setNum(MG, 16);
00183               if (tmp.length() < 2)
00184                      tmp.insert(0, "0");
00185               name += tmp;
00186               tmp.setNum(YB, 16);
00187               if (tmp.length() < 2)
00188                      tmp.insert(0, "0");
00189               name += tmp;
00190               break;
00191        }
00192        return name;
00193 }
00194 
00195 QString ScColor::nameCMYK(const ScribusDoc* doc)
00196 {
00197        if ((Model != colorModelCMYK) && (!doc))
00198               qDebug("calling nameCMYK with a rgb color");
00199        CMYKColor cmyk;
00200        int c, m, y, k;
00201        QString tmp, name = CommonStrings::None;
00202        ScColorEngine::getCMYKValues(*this, doc, cmyk);
00203        cmyk.getValues(c, m, y, k);
00204        name="#";
00205        tmp.setNum(c, 16);
00206        if (tmp.length() < 2)
00207               tmp.insert(0, "0");
00208        name += tmp;
00209        tmp.setNum(m, 16);
00210        if (tmp.length() < 2)
00211               tmp.insert(0, "0");
00212        name += tmp;
00213        tmp.setNum(y, 16);
00214        if (tmp.length() < 2)
00215               tmp.insert(0, "0");
00216        name += tmp;
00217        tmp.setNum(k, 16);
00218        if (tmp.length() < 2)
00219               tmp.insert(0, "0");
00220        name += tmp;
00221        return name;
00222 }
00223 
00224 QString ScColor::nameRGB(const ScribusDoc* doc)
00225 {
00226        if ((Model != colorModelRGB) && (!doc))
00227               qDebug("calling nameRGB with a cmyk color");
00228        int r, g, b;
00229        RGBColor rgb;
00230        QString tmp, name = CommonStrings::None;
00231        ScColorEngine::getRGBValues(*this, doc, rgb);
00232        rgb.getValues(r, g, b);
00233        name="#";
00234        tmp.setNum(r, 16);
00235        if (tmp.length() < 2)
00236               tmp.insert(0, "0");
00237        name += tmp;
00238        tmp.setNum(g, 16);
00239        if (tmp.length() < 2)
00240               tmp.insert(0, "0");
00241        name += tmp;
00242        tmp.setNum(b, 16);
00243        if (tmp.length() < 2)
00244               tmp.insert(0, "0");
00245        name += tmp;
00246        return name;
00247 }
00248 
00249 void ScColor::setNamedColor(QString name)
00250 {
00251        bool ok;
00252        if (name.length () == 9)
00253        {
00254               int c = name.mid(1,2).toInt(&ok, 16);
00255               int m = name.mid(3,2).toInt(&ok, 16);
00256               int y = name.mid(5,2).toInt(&ok, 16);
00257               int k = name.mid(7,2).toInt(&ok, 16);
00258               setColor(c, m, y, k);
00259        }
00260        else if (name.length () == 7)
00261        {
00262               int r = name.mid(1,2).toInt(&ok, 16);
00263               int g = name.mid(3,2).toInt(&ok, 16);
00264               int b = name.mid(5,2).toInt(&ok, 16);
00265               setColorRGB(r, g, b);
00266        }
00267 }
00268 
00269 bool ScColor::isRegistrationColor() const
00270 {
00271        return Regist;
00272 }
00273 
00274 void ScColor::setRegistrationColor(bool s)
00275 {
00276        Regist = s;
00277 }
00278 
00279 bool ScColor::isSpotColor() const
00280 {
00281        return Spot;
00282 }
00283 
00284 void ScColor::setSpotColor(bool s)
00285 {
00286        Spot = s;
00287 }
00288 
00289 ColorList::ColorList(ScribusDoc* doc, bool retainDoc) : QMap<QString,ScColor>()
00290 {
00291        m_doc = doc;
00292        m_retainDoc = retainDoc;
00293 }
00294 
00295 void ColorList::setDocument(ScribusDoc* doc)
00296 {
00297        m_doc = doc;
00298 }
00299 
00300 ColorList& ColorList::operator= (const ColorList& list)
00301 {
00302        clear();
00303        if (!m_retainDoc)
00304               m_doc = list.m_doc;
00305        addColors(list);
00306        return *this;
00307 }
00308 
00309 void ColorList::addColors(const ColorList& colorList, bool overwrite)
00310 {
00311        ColorList::ConstIterator it;
00312        ColorList::ConstIterator itend;
00313        itend = colorList.end();
00314        for (it = colorList.begin(); it != itend; ++it)
00315               insert(it.key(), it.data(), overwrite);
00316 }
00317 
00318 void ColorList::copyColors(const ColorList& colorList, bool overwrite)
00319 {
00320        clear();
00321        addColors(colorList, overwrite);
00322 }
00323 
00324 void ColorList::ensureBlackAndWhite(void)
00325 {
00326        ensureBlack();
00327        ensureWhite();
00328 }
00329 
00330 void ColorList::ensureBlack(void)
00331 {
00332        bool addBlack = true;
00333        ColorList::Iterator itb = find("Black");
00334        if (itb != end())
00335        {
00336               ScColor& black = itb.data();
00337               colorModel model = black.getColorModel();
00338               if (model == colorModelCMYK)
00339               {
00340                      int c, m, y, k;
00341                      black.getCMYK(&c, &m, &y, &k);
00342                      if (c == 0 && m == 0 && y == 0 && k == 255)
00343                             addBlack = false;
00344               }
00345        }
00346        if (addBlack)
00347               insert("Black", ScColor(0, 0, 0, 255));
00348 }
00349 
00350 void ColorList::ensureWhite(void)
00351 {
00352        bool addWhite = true;
00353        ColorList::Iterator itw = find("White");
00354        if (itw != end())
00355        {
00356               ScColor& white = itw.data();
00357               colorModel model = white.getColorModel();
00358               if (model == colorModelCMYK)
00359               {
00360                      int c, m, y, k;
00361                      white.getCMYK(&c, &m, &y, &k);
00362                      if (c == 0 && m == 0 && y == 0 && k == 0)
00363                             addWhite = false;
00364               }
00365        }
00366        if (addWhite)
00367               insert("White", ScColor(0, 0, 0, 0));
00368 }