Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Defines | Functions
colorutil.cpp File Reference
#include "colorutil.h"
#include <qbitmap.h>
#include <qdatetime.h>
#include <qpainter.h>
#include <qcheckbox.h>
#include "scconfig.h"
#include "md5.h"
#include "scribusdoc.h"
#include "scpixmapcache.h"
#include "commonstrings.h"
#include "util.h"
#include "sccolorengine.h"
#include <jpeglib.h>
#include <jerror.h>

Go to the source code of this file.

Defines

#define XMD_H

Functions

void handleOldColorShade (ScribusDoc *doc, QString &colName, int &shade)
 Associate a color shade to a document color according to pre-1.3.4cvs rgb shade calculation method, creating new document color if necessary.
QColor getOldColorShade (const QColor &color, int shade)
 Compute color shade of a rgb color using pre-1.3.4 method.
QColor SCRIBUS_API getOldColorShade (uchar red, uchar green, uchar blue, int shade)
 Compute color shade of a rgb color using pre-1.3.4 method.
QImage ProofImage (QImage *Image, ScribusDoc *doc)
QColor SetColor (ScribusDoc *currentDoc, QString color, int shad)
QPixmap * getSmallPixmap (QColor rgb)
 QPixmaps are really slow with Qt/Mac 3.3.4.
QPixmap * getWidePixmap (QColor rgb)
static Q_UINT64 code64 (const ScColor &col)
QPixmap * getFancyPixmap (const ScColor &col, ScribusDoc *doc)
void paintAlert (QPixmap &toPaint, QPixmap &target, int x, int y, bool useMask)
 Put toPaint pixmap into target at the x, y place.
unsigned char INT_MULT (unsigned char a, unsigned char b)
 Convert a color in RGB space to HSV space (Hue, Saturation, Value).
void RGBTOHSV (uchar &red, uchar &green, uchar &blue)
 Convert a color in HSV space to RGB space.
void HSVTORGB (uchar &hue, uchar &saturation, uchar &value)
 Convert a color in RGB space to HLS space (Hue, Lightness, Saturation).
void RGBTOHLS (uchar &red, uchar &green, uchar &blue)
double HLSVALUE (double n1, double n2, double hue)
 Implement the HLS "double hex-cone".
void HLSTORGB (uchar &hue, uchar &lightness, uchar &saturation)
 Convert a color in HLS space to RGB space.
double getCurveYValue (FPointArray &curve, double x, bool linear)
double Lum (uchar red, uchar green, uchar blue)
double LumD (double red, double green, double blue)
void setLum (uchar &red, uchar &green, uchar &blue, double lum)
void clipColor (double &red, double &green, double &blue)

Define Documentation

#define XMD_H

Definition at line 69 of file colorutil.cpp.


Function Documentation

void clipColor ( double &  red,
double &  green,
double &  blue 
)

Definition at line 592 of file colorutil.cpp.

{
       double l = LumD(red, green, blue);
       double n = QMIN(red, QMIN(green, blue));
       double x = QMAX(red, QMAX(green, blue));
       if (n < 0.0)
       {
              red = l + (((red - l) * l) / (l - n));
              green = l + (((green - l) * l) / (l - n));
              blue = l + (((blue - l) * l) / (l - n));
       }
       if (x > 1.0)
       {
              red = l + (((red - l) * (1.0 - l)) / (x - l));
              green = l + (((green - l) * (1.0 - l)) / (x - l));
              blue = l + (((blue - l) * (1.0 - l)) / (x - l));
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static Q_UINT64 code64 ( const ScColor &  col) [static]

Definition at line 223 of file colorutil.cpp.

                                            {
       int C, M, Y, K, R, G, B;
       Q_UINT64 result=0;
       col.getRGB( &R, &G, &B );
       col.getCMYK( &C, &M, &Y, &K );
       result |= col.getColorModel() == colorModelRGB ? 1 : 0;
       result |= col.isSpotColor() ? 64 : 0;
       result |= col.isRegistrationColor() ? 32 : 0;
       result <<= 8;
       result |= C;
       result <<= 8;
       result |= M;
       result <<= 8;
       result |= Y;
       result <<= 8;
       result |= K;
       result <<= 8;
       result |= R;
       result <<= 8;
       result |= G;
       result <<= 8;
       result |= B;
       return result;
}

Here is the caller graph for this function:

double getCurveYValue ( FPointArray &  curve,
double  x,
bool  linear 
)

Definition at line 504 of file colorutil.cpp.

{
    double t;
    FPoint p;
    FPoint p0,p1,p2,p3;
    double c0,c1,c2,c3;
    double val = 0.5;
    if(curve.size() == 0)
        return 0.5;
    // First find curve segment
    p = curve.point(0);
    if(x < p.x())
        return p.y();
    p = curve.point(curve.size()-1);
    if(x >= p.x())
        return p.y();
       uint cc = 0;
    // Find the four control points (two on each side of x)    
    p = curve.point(0);
    while(x >= p.x())
    {
              cc++;
        p = curve.point(cc);
    }
    if (cc > 1)
    {
       p0 = curve.point(cc-2);
       p1 = curve.point(cc-1);
    }
    else
        p1 = p0 = curve.point(0);
    p2 = p;
    if (cc < curve.size()-1)
       p3 = curve.point(cc+1);
    else
       p3 = p;
    // Calculate the value
       if (linear)
       {
              double mc;
              if (p1.x() - p2.x() != 0.0)
                     mc = (p1.y() - p2.y()) / (p1.x() - p2.x());
              else
                     mc = p2.y() / p2.x();
              val = (x - p1.x()) * mc + p1.y();
       }
       else
       {
              t = (x - p1.x()) / (p2.x() - p1.x());
              c2 = (p2.y() - p0.y()) * (p2.x()-p1.x()) / (p2.x()-p0.x());
              c3 = p1.y();
              c0 = -2*p2.y() + 2*c3 + c2 + (p3.y() - p1.y()) * (p2.x() - p1.x()) / (p3.x() - p1.x());
              c1 = p2.y() - c3 - c2 - c0;
              val = ((c0*t + c1)*t + c2)*t + c3;
       }
       if(val < 0.0)
              val = 0.0;
       if(val > 1.0)
              val = 1.0;
       return val;
}

Here is the caller graph for this function:

QPixmap* getFancyPixmap ( const ScColor &  col,
ScribusDoc *  doc 
)

Definition at line 248 of file colorutil.cpp.

                                                              {
       static ScPixmapCache<Q_UINT64> pxCache;

       static QPixmap alertIcon;
       static QPixmap cmykIcon;
       static QPixmap rgbIcon;
       static QPixmap spotIcon;
       static QPixmap regIcon;
       static bool iconsInitialized = false;

       if ( !iconsInitialized ) {
              alertIcon = loadIcon("alert.png");
              cmykIcon = loadIcon("cmyk.png");
              rgbIcon = loadIcon("rgb.png");
              spotIcon = loadIcon("spot.png");
              regIcon = loadIcon("register.png");
              iconsInitialized = true;
       }

       Q_UINT64 res=code64(col);
       if (pxCache.contains(res))
              return pxCache[res];

       QPixmap *pa=new QPixmap(60, 15);
       QPixmap *pm=getSmallPixmap(col.getRawRGBColor());
       pa->fill(Qt::white);
       paintAlert(*pm, *pa, 0, 0);
       if (ScColorEngine::isOutOfGamut(col, doc))
              paintAlert(alertIcon, *pa, 15, 0);
       if ((col.getColorModel() == colorModelCMYK) || (col.isSpotColor()))
              paintAlert(cmykIcon, *pa, 30, 0);
       else
              paintAlert(rgbIcon, *pa, 30, 0);
       if (col.isSpotColor())
              paintAlert(spotIcon, *pa, 46, 2);
       if (col.isRegistrationColor())
              paintAlert(regIcon, *pa, 45, 0);
       pxCache.insert(res, pa);
       return pa;
}

Here is the call graph for this function:

QColor getOldColorShade ( const QColor &  color,
int  shade 
)

Compute color shade of a rgb color using pre-1.3.4 method.

Parameters:
colorthe base color
shadeshade value.

Definition at line 123 of file colorutil.cpp.

{
       int r, g, b;
       color.getRgb(&r, &g, &b);
       return getOldColorShade(r, g, b, shade);
}

Here is the call graph for this function:

Here is the caller graph for this function:

QColor SCRIBUS_API getOldColorShade ( uchar  red,
uchar  green,
uchar  blue,
int  shade 
)

Compute color shade of a rgb color using pre-1.3.4 method.

Parameters:
redthe red component.
greenthe green component.
bluethe blue component.
shadeshade value.

Definition at line 130 of file colorutil.cpp.

{
       int h, s, v, snew;
       QColor color(red, green, blue);
       color.hsv(&h, &s, &v);
       if (red == green && green == blue)
       {
              snew = 255 - ((255 - v) * shade / 100);
              color.setHsv(h, s, snew);
       }
       else
       {
              snew = s * shade / 100;
              color.setHsv(h, snew, v);
       }
       return color;
}
QPixmap* getSmallPixmap ( QColor  rgb)

QPixmaps are really slow with Qt/Mac 3.3.4.

Create a cool all-infos pixmaps for the specified color.

Really, really, slow. So we better cache them.

Definition at line 188 of file colorutil.cpp.

{
       static ScPixmapCache<QRgb> pxCache;

       QRgb index=rgb.rgb();
       if (pxCache.contains(index))
              return pxCache[index];

       QPixmap *pm = new QPixmap(15, 15);
       pm->fill(rgb);
       QPainter p;
       p.begin(pm);
       p.setBrush(Qt::NoBrush);
       QPen b(Qt::black, 1);
       p.setPen(b);
       p.drawRect(0, 0, 15, 15);
       p.end();
       pxCache.insert(index, pm);
       return pm;
}

Here is the caller graph for this function:

QPixmap* getWidePixmap ( QColor  rgb)

Definition at line 209 of file colorutil.cpp.

{
       static ScPixmapCache<QRgb> pxCache;

       QRgb index=rgb.rgb();
       if (pxCache.contains(index))
              return pxCache[index];

       QPixmap *pm = new QPixmap(30, 15);
       pm->fill(rgb);
       pxCache.insert(index, pm);
       return pm;
}

Here is the caller graph for this function:

void handleOldColorShade ( ScribusDoc *  doc,
QString &  colName,
int shade 
)

Associate a color shade to a document color according to pre-1.3.4cvs rgb shade calculation method, creating new document color if necessary.

Parameters:
docthe scribus document
colNamethe color name (modified in place)
shadethe color level (modified in place)

Definition at line 84 of file colorutil.cpp.

{
       int r, g, b;
       bool found = false;
       if( colName.isEmpty() || colName == CommonStrings::None || !doc->PageColors.contains(colName))
              return;

       const ScColor& scCol1(doc->PageColors[colName]);
       
       if( (shade == 100) || (scCol1.getColorModel() != colorModelRGB) )
              return;
       scCol1.getRGB(&r, &g, &b);
       QColor col1 = getOldColorShade(r, g, b, shade), col2;
       ColorList::Iterator it, itEnd = doc->PageColors.end();
       for( it = doc->PageColors.begin(); it != itEnd; it++)
       {
              if ( it.data().getColorModel() == colorModelRGB )
              {
                     it.data().getRGB(&r, &g, &b);
                     col2.setRgb(r, g, b);
                     if( col1 == col2 )
                     {
                            found = true;
                            break;
                     }
              }
       }
       if(!found)
       {
              ScColor tmp;
              tmp.fromQColor(col1);
              colName = QString("%1 %2%").arg(colName).arg(shade);
              doc->PageColors.insert(colName, tmp);
       }
       else
              colName = it.key();
       shade = 100;
}

Here is the call graph for this function:

void HLSTORGB ( uchar hue,
uchar lightness,
uchar saturation 
)

Convert a color in HLS space to RGB space.

Parameters:
huethe hue component (modified in place).
lightnessthe lightness component (modified in place).
saturationthe saturation component (modified in place).

Definition at line 481 of file colorutil.cpp.

{
       double H = ( hue / 255.0 );
       double L = ( lightness / 255.0 );
       double S = ( saturation / 255.0 );
       if (S == 0)
       {
              hue = qRound(255 * L);
              saturation = qRound(255 * L);
              return;
       }
       double var_1 = 0;
       double var_2 = 0;
       if ( L < 0.5 )
              var_2 = L * ( 1 + S );
       else
              var_2 = ( L + S ) - ( S * L );
       var_1 = 2 * L - var_2;
       hue = qRound(255 * HLSVALUE( var_1, var_2, H + ( 1.0 / 3.0 ) ));
       lightness = qRound(255 * HLSVALUE( var_1, var_2, H ));
       saturation = qRound(255 * HLSVALUE( var_1, var_2, H - ( 1.0 / 3.0 ) ));
}

Here is the call graph for this function:

Here is the caller graph for this function:

double HLSVALUE ( double  n1,
double  n2,
double  hue 
)

Implement the HLS "double hex-cone".

Parameters:
n1lightness fraction (?)
n2saturation fraction (?)
huehue "angle".
Returns:
HLS value.

Definition at line 466 of file colorutil.cpp.

{
       if ( hue < 0 )
              hue += 1;
       if ( hue > 1 )
              hue -= 1;
       if ( ( 6 * hue ) < 1 )
              return n1 + ( n2 - n1 ) * 6 * hue;
       if ( ( 2 * hue ) < 1 )
              return n2;
       if ( ( 3 * hue ) < 2 )
              return n1 + ( n2 - n1 ) * ( ( 2.0 / 3.0 ) - hue ) * 6;
       return n1;
}

Here is the caller graph for this function:

void HSVTORGB ( uchar hue,
uchar saturation,
uchar value 
)

Convert a color in RGB space to HLS space (Hue, Lightness, Saturation).

Parameters:
redthe red component (modified in place).
greenthe green component (modified in place).
bluethe blue component (modified in place).

Definition at line 367 of file colorutil.cpp.

{
       if ( saturation == 0 )
       {
              hue        = value;
              saturation = value;
              value      = value;
       }
       else
       {
              double h = hue * 6. / 255.;
              double s = saturation / 255.;
              double v = value / 255.;

              double f = h - (int)h;
              double p = v * ( 1. - s );
              double q = v * ( 1. - ( s * f ) );
              double t = v * ( 1. - ( s * ( 1. - f ) ) );
              // Worth a note here that gcc 2.96 will generate different results
              // depending on optimization mode on i386.
              switch ((int)h)
              {
              case 0:
                     hue        = (uchar)( v * 255 );
                     saturation = (uchar)( t * 255 );
                     value      = (uchar)( p * 255 );
                     break;
              case 1:
                     hue        = (uchar)( q * 255 );
                     saturation = (uchar)( v * 255 );
                     value      = (uchar)( p * 255 );
                     break;
              case 2:
                     hue        = (uchar)( p * 255 );
                     saturation = (uchar)( v * 255 );
                     value      = (uchar)( t * 255 );
                     break;
              case 3:
                     hue        = (uchar)( p * 255 );
                     saturation = (uchar)( q * 255 );
                     value      = (uchar)( v * 255 );
                     break;
              case 4:
                     hue        = (uchar)( t * 255 );
                     saturation = (uchar)( p * 255 );
                     value      = (uchar)( v * 255 );
                     break;
              case 5:
                     hue        = (uchar)( v * 255 );
                     saturation = (uchar)( p * 255 );
                     value      = (uchar)( q * 255 );
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char INT_MULT ( unsigned char  a,
unsigned char  b 
)

Convert a color in RGB space to HSV space (Hue, Saturation, Value).

Parameters:
redthe red component (modified in place).
greenthe green component (modified in place).
bluethe blue component (modified in place).

Definition at line 315 of file colorutil.cpp.

{
       int c = a * b + 0x80;
       return (unsigned char)(( ( c >> 8 ) + c ) >> 8);
}

Here is the caller graph for this function:

double Lum ( uchar  red,
uchar  green,
uchar  blue 
)

Definition at line 566 of file colorutil.cpp.

{
       return 0.3 * (red / 255.0) + 0.59 * (green / 255.0) + 0.11 * (blue / 255.0);
}

Here is the caller graph for this function:

double LumD ( double  red,
double  green,
double  blue 
)

Definition at line 571 of file colorutil.cpp.

{
       return 0.3 * red + 0.59 * green + 0.11 * blue;
}

Here is the caller graph for this function:

void paintAlert ( QPixmap &  toPaint,
QPixmap &  target,
int  x = 0,
int  y = 0,
bool  useMask = true 
)

Put toPaint pixmap into target at the x, y place.

There is handled the alpha channel/transparency too. In the beginning is the target pixmap filled with all_transparent mask. In the case of the painting of tiPaint into x, y place, there is this pixmap enabled in alpha mask too.

Parameters:
toPainta pixmap to be painted into target
targeta base pixmap. Some kind of painter.
xcoordinate
ycoordinate

Definition at line 290 of file colorutil.cpp.

{
       // there is no alpha mask in the beginning
       if (useMask)
       {
              if (target.mask()==0)
                     target.setMask(QBitmap(target.width(), target.height(), useMask));
       }
       QPainter p;
       p.begin(&target);
       p.drawPixmap(x, y, toPaint);
       if (useMask)
       {
              QPainter alpha; // transparency handling
              alpha.begin(target.mask());
              alpha.setBrush(Qt::color1);
              alpha.setPen(Qt::color1);
              alpha.drawRect(x, y, 15, 15);
              if (toPaint.mask() != 0)
                     alpha.drawPixmap(x, y, *toPaint.mask());
              alpha.end();
       }
       p.end();
}

Here is the caller graph for this function:

QImage ProofImage ( QImage *  Image,
ScribusDoc *  doc 
)

Definition at line 148 of file colorutil.cpp.

{
       QImage out = Image->copy();
       bool cmsUse = doc ? doc->HasCMS : false;
       bool softProofing = doc ? doc->SoftProofing : false;
       if (cmsUse && softProofing)
       {
              int outheight=out.height();
              for (int i=0; i < outheight; ++i)
              {
                     LPBYTE ptr = out.scanLine(i);
                     cmsDoTransform(doc->stdProofImg, ptr, ptr, out.width());
              }
       }
       else
       {
              if (cmsUse)
              {
                     int outheight=out.height();
                     for (int i=0; i < outheight; ++i)
                     {
                            LPBYTE ptr = out.scanLine(i);
                            cmsDoTransform(doc->stdTransImg, ptr, ptr, out.width());
                     }
              }
       }
       return out;
}
void RGBTOHLS ( uchar red,
uchar green,
uchar blue 
)

Definition at line 422 of file colorutil.cpp.

{
       double var_R = ( red / 255.0 );
       double var_G = ( green / 255.0 );
       double var_B = ( blue / 255.0 );
       double var_Min = QMIN( var_R, QMIN(var_G, var_B) );    //Min. value of RGB
       double var_Max = QMAX( var_R, QMAX(var_G, var_B) );    //Max. value of RGB
       double del_Max = var_Max - var_Min;             //Delta RGB value
       double L = ( var_Max + var_Min ) / 2.0;
       double H = 0;
       double S = 0;
       double del_R = 0;
       double del_G = 0;
       double del_B = 0;
       if ( del_Max == 0 )
       {
              H = 0;
              S = 0;
       }
       else
       {
              if ( L < 0.5 )
                     S = del_Max / ( var_Max + var_Min );
              else
                     S = del_Max / ( 2 - var_Max - var_Min );
              del_R = ( ( ( var_Max - var_R ) / 6.0 ) + ( del_Max / 2.0 ) ) / del_Max;
              del_G = ( ( ( var_Max - var_G ) / 6.0 ) + ( del_Max / 2.0 ) ) / del_Max;
              del_B = ( ( ( var_Max - var_B ) / 6.0 ) + ( del_Max / 2.0 ) ) / del_Max;
              if ( var_R == var_Max )
                     H = del_B - del_G;
          else if ( var_G == var_Max )
                     H = ( 1.0 / 3.0 ) + del_R - del_B;
              else if ( var_B == var_Max )
                     H = ( 2.0 / 3.0 ) + del_G - del_R;
              if ( H < 0 )
                     H += 1;
              if ( H > 1 )
                     H -= 1;
       }
       red = qRound(H * 255);
       green = qRound(L * 255);
       blue = qRound(S * 255);
}

Here is the caller graph for this function:

void RGBTOHSV ( uchar red,
uchar green,
uchar blue 
)

Convert a color in HSV space to RGB space.

Parameters:
huethe hue component (modified in place).
saturationthe saturation component (modified in place).
valuethe value component (modified in place).

Definition at line 321 of file colorutil.cpp.

{
       int r, g, b;
       double h, s, v;
       int min, max;
       h = 0.;
       r = red;
       g = green;
       b = blue;
       if ( r > g )
       {
              max = QMAX( r, b );
              min = QMIN( g, b );
       }
       else
       {
              max = QMAX( g, b );
              min = QMIN( r, b );
       }
       v = max;
       if ( max != 0 )
              s = ( ( max - min ) * 255 ) / (double)max;
       else
              s = 0;
       if ( s == 0 )
              h = 0;
       else
       {
              int delta = max - min;
              if ( r == max )
                     h = ( g - b ) / (double)delta;
              else if ( g == max )
                     h = 2 + ( b - r ) / (double)delta;
              else if ( b == max )
                     h = 4 + ( r - g ) / (double)delta;
              h *= 42.5;
              if ( h < 0 )
                     h += 255;
              if ( h > 255 )
                     h -= 255;
       }
       red   = (uchar)h;
       green = (uchar)s;
       blue  = (uchar)v;
}

Here is the caller graph for this function:

QColor SetColor ( ScribusDoc *  currentDoc,
QString  color,
int  shad 
)

Definition at line 177 of file colorutil.cpp.

{
       const ScColor& col = currentDoc->PageColors[color];
       return ScColorEngine::getShadeColorProof(col, currentDoc, shad);
}
void setLum ( uchar red,
uchar green,
uchar blue,
double  lum 
)

Definition at line 576 of file colorutil.cpp.

{
       double rP = (red / 255.0);
       double gP = (green / 255.0);
       double bP = (blue / 255.0);
       double d = lum - Lum(red, green, blue);
       rP += d;
       gP += d;
       bP += d;
       clipColor(rP, gP, bP);
       red = qRound(rP * 255);
       green = qRound(gP * 255);
       blue = qRound(bP * 255);
       return;
}

Here is the call graph for this function: