Logo Search packages:      
Sourcecode: codeblocks version File versions  Download package

pdfcolor.cpp

Go to the documentation of this file.
///////////////////////////////////////////////////////////////////////////////
// Name:        pdfcolor.cpp
// Purpose:     Implementation of wxPdfDocument color handling
// Author:      Ulrich Telle
// Modified by:
// Created:     2006-01-27
// Copyright:   (c) Ulrich Telle
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

/// \file pdfcolor.cpp Implementation of the wxPdfDocument color handling

// For compilers that support precompilation, includes <wx/wx.h>.
#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include "wx/pdfdoc.h"

#include "pdfcolordata.inc"

wxColourDatabase* wxPdfColour::ms_colorDatabase = NULL;

wxColourDatabase*
00031 wxPdfColour::GetColorDatabase()
{
  if (ms_colorDatabase == NULL)
  {
    if (wxTheColourDatabase != NULL)
    {
      ms_colorDatabase = wxTheColourDatabase;
    }
    else
    {
      static wxColourDatabase pdfColorDatabase;
      ms_colorDatabase = &pdfColorDatabase;
    }
    size_t n;
    for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
    {
      const wxColourDesc& cc = wxColourTable[n];
      ms_colorDatabase->AddColour(cc.name, wxColour(cc.r, cc.g, cc.b));
    }
  }
  return ms_colorDatabase;
}

00054 wxPdfColour::wxPdfColour()
{
  m_type   = wxPDF_COLOURTYPE_UNKNOWN;
  m_prefix = wxEmptyString;
  m_color  = _T("0");
}

00061 wxPdfColour::wxPdfColour(const unsigned char grayscale)
{
  SetColor(grayscale);
}

00066 wxPdfColour::wxPdfColour(const wxColour& color)
{
  SetColor(color);
}

00071 wxPdfColour::wxPdfColour(const unsigned char red, const unsigned char green, const unsigned char blue)
{
  SetColor(red, green, blue);
}

00076 wxPdfColour::wxPdfColour(double cyan, double magenta, double yellow, double black)
{
  SetColor(cyan, magenta, yellow, black);
}

00081 wxPdfColour::wxPdfColour(const wxPdfSpotColour& spot, double tint)
{
  SetColor(spot, tint);
}

00086 wxPdfColour::wxPdfColour(const wxString& name)
{
  SetColor(name);
}

00091 wxPdfColour::wxPdfColour(const wxPdfColour& color)
{
  m_type   = color.m_type;
  m_prefix = color.m_prefix;
  m_color  = color.m_color;
}

00098 wxPdfColour::wxPdfColour(const wxString& color, bool WXUNUSED(internal))
{
  m_color = color;
}

wxPdfColour&
00104 wxPdfColour::operator=(const wxPdfColour& color)
{
  m_type   = color.m_type;
  m_prefix = color.m_prefix;
  m_color  = color.m_color;
  return *this;
}

bool
00113 wxPdfColour::Equals(const wxPdfColour& color) const
{
  return (m_type == color.m_type) && (m_prefix == color.m_prefix) && (m_color == color.m_color);
}

void
00119 wxPdfColour::SetColor(const unsigned char grayscale)
{
  m_type   = wxPDF_COLOURTYPE_GRAY;
  m_prefix = wxEmptyString;
  m_color  = wxPdfDocument::Double2String(((double) grayscale)/255.,3);
}

void
00127 wxPdfColour::SetColor(const wxColour& color)
{
  m_type   = wxPDF_COLOURTYPE_RGB;
  m_prefix = wxEmptyString;
  m_color  = wxPdfDocument::RGB2String(color);
}

void
00135 wxPdfColour::SetColor(const unsigned char red, const unsigned char green, const unsigned char blue)
{
  SetColor(wxColour(red,green,blue));
}

void
00141 wxPdfColour::SetColor(double cyan, double magenta, double yellow, double black)
{
  m_type   = wxPDF_COLOURTYPE_CMYK;
  m_prefix = wxEmptyString;
  m_color  = wxPdfDocument::Double2String(wxPdfDocument::ForceRange(cyan,    0., 100.)/100.,3) + _T(" ") +
             wxPdfDocument::Double2String(wxPdfDocument::ForceRange(magenta, 0., 100.)/100.,3) + _T(" ") +
             wxPdfDocument::Double2String(wxPdfDocument::ForceRange(yellow,  0., 100.)/100.,3) + _T(" ") +
             wxPdfDocument::Double2String(wxPdfDocument::ForceRange(black,   0., 100.)/100.,3);
}

void
00152 wxPdfColour::SetColor(const wxString& name)
{
  if (name.Length() == 7 && name[0] == wxT('#'))
  {
    unsigned long r = 0, g = 0, b = 0;
    if (name.Mid(1,2).ToULong(&r,16) &&
        name.Mid(3,2).ToULong(&g,16) &&
        name.Mid(5,2).ToULong(&b,16))
    {
      SetColor((unsigned char) r, (unsigned char) g, (unsigned char) b);
    }
    else
    {
     SetColor(0);
    }
  }
  else
  {
    wxColourDatabase* colorDatabase = GetColorDatabase();
    wxColour color = colorDatabase->Find(name);
    if (color.Ok())
    {
      SetColor(color);
    }
    else
    {
      SetColor(0);
    }
  }
}

void
00184 wxPdfColour::SetColor(const wxPdfSpotColour& spot, double tint)
{
  m_type   = wxPDF_COLOURTYPE_SPOT;
  m_prefix = wxString::Format(_T("/CS%d CS "), spot.GetIndex());
  m_color  = wxPdfDocument::Double2String(wxPdfDocument::ForceRange(tint, 0., 100.)/100.,3);
}

const wxString
00192 wxPdfColour::GetColor(bool drawing) const
{
  wxString color = wxEmptyString;
  switch (m_type)
  {
    case wxPDF_COLOURTYPE_GRAY:
      color = m_color + wxString(_T(" g"));
      break;
    case wxPDF_COLOURTYPE_RGB:
      color = m_color + wxString(_T(" rg"));
      break;
    case wxPDF_COLOURTYPE_CMYK:
      color = m_color + wxString(_T(" k"));
      break;
    case wxPDF_COLOURTYPE_SPOT:
      color = m_prefix + m_color + wxString(_T(" scn"));
      break;
    default:
      color = wxString(_T("0 g"));
      break;
  }
  if (drawing)
    color.MakeUpper();
  else
    color.MakeLower();
  color.Replace(_T("/cs"), _T("/CS"));
  return color;
}

const wxString
00222 wxPdfColour::GetColorValue() const
{
  return m_color;
}

00227 wxPdfSpotColour::wxPdfSpotColour(int index, double cyan, double magenta, double yellow, double black)
  : m_objIndex(0), m_index(index), m_cyan(cyan), m_magenta(magenta), m_yellow(yellow), m_black(black)
{
}

00232 wxPdfSpotColour::wxPdfSpotColour(const wxPdfSpotColour& color)
{
  m_objIndex = color.m_objIndex;
  m_index    = color.m_index;
  m_cyan     = color.m_cyan;
  m_magenta  = color.m_magenta;
  m_yellow   = color.m_yellow;
  m_black    = color.m_black;
}

// ---

void
00245 wxPdfDocument::AddSpotColor(const wxString& name, double cyan, double magenta, double yellow, double black)
{
  wxPdfSpotColourMap::iterator spotColor = (*m_spotColors).find(name);
  if (spotColor == (*m_spotColors).end())
  {
    int i = (*m_spotColors).size() + 1;
    (*m_spotColors)[name] = new wxPdfSpotColour(i, cyan, magenta, yellow, black);
  }
}
 
void
00256 wxPdfDocument::SetDrawColor(const wxColour& color)
{
  wxPdfColour tempColor(color);
  m_drawColor = tempColor;
  if (m_page > 0)
  {
    OutAscii(m_drawColor.GetColor(true));
  }
}

void
00267 wxPdfDocument::SetDrawColor(const unsigned char grayscale)
{
  wxPdfColour tempColor(grayscale);
  m_drawColor = tempColor;
  if (m_page > 0)
  {
    OutAscii(m_drawColor.GetColor(true));
  }
}

void
00278 wxPdfDocument::SetDrawColor(const unsigned char red, const unsigned char green, const unsigned char blue)
{
  SetDrawColor(wxColour(red, green, blue));
}

void
00284 wxPdfDocument::SetDrawColor(double cyan, double magenta, double yellow, double black)
{
  SetDrawColor(wxPdfColour(cyan, magenta, yellow, black));
}

void
00290 wxPdfDocument::SetDrawColor(const wxPdfColour& color)
{
  m_drawColor = color;
  if (m_page > 0)
  {
    OutAscii(m_drawColor.GetColor(true));
  }
}

void
00300 wxPdfDocument::SetDrawColor(const wxString& name, double tint)
{
  wxPdfSpotColourMap::iterator spotColor = (*m_spotColors).find(name);
  if (spotColor != (*m_spotColors).end())
  {
    wxPdfColour tempColor(*(spotColor->second), tint);
    m_drawColor = tempColor;
    if (m_page > 0)
    {
      OutAscii(m_drawColor.GetColor(true));
    }
  }
  else
  {
    wxLogError(_("SetDrawColor: Undefined spot color: ") + name);
  }
}

const wxPdfColour
00319 wxPdfDocument::GetDrawColor()
{
  return wxPdfColour(m_drawColor);
}

void
00325 wxPdfDocument::SetFillColor(const wxColour& color)
{
  wxPdfColour tempColor(color);
  m_fillColor = tempColor;
  m_colorFlag = (m_fillColor != m_textColor);
  if (m_page > 0)
  {
    OutAscii(m_fillColor.GetColor(false));
  }
}

void
00337 wxPdfDocument::SetFillColor(const unsigned char grayscale)
{
  wxPdfColour tempColor(grayscale);
  m_fillColor = tempColor;
  m_colorFlag = (m_fillColor != m_textColor);
  if (m_page > 0)
  {
    OutAscii(m_fillColor.GetColor(false));
  }
}

void
00349 wxPdfDocument::SetFillColor(const wxPdfColour& color)
{
  m_fillColor = color;
  m_colorFlag = (m_fillColor != m_textColor);
  if (m_page > 0)
  {
    OutAscii(m_fillColor.GetColor(false));
  }
}

void
00360 wxPdfDocument::SetFillColor(const unsigned char red, const unsigned char green, const unsigned char blue)
{
  SetFillColor(wxColour(red, green, blue));
}

void
00366 wxPdfDocument::SetFillColor(double cyan, double magenta, double yellow, double black)
{
  SetFillColor(wxPdfColour(cyan, magenta, yellow, black));
}

void
00372 wxPdfDocument::SetFillColor(const wxString& name, double tint)
{
  wxPdfSpotColourMap::iterator spotColor = (*m_spotColors).find(name);
  if (spotColor != (*m_spotColors).end())
  {
    wxPdfColour tempColor(*(spotColor->second), tint);
    m_fillColor = tempColor;
    m_colorFlag = (m_fillColor != m_textColor);
    if (m_page > 0)
    {
      OutAscii(m_fillColor.GetColor(false));
    }
  }
  else
  {
    wxLogError(_("SetFillColor: Undefined spot color: ") + name);
  }
}

const wxPdfColour
00392 wxPdfDocument::GetFillColor()
{
  return wxPdfColour(m_fillColor);
}

void
00398 wxPdfDocument::SetTextColor(const wxColour& color)
{
  wxPdfColour tempColor(color);
  m_textColor = tempColor;
  m_colorFlag = (m_fillColor != m_textColor);
}

void
00406 wxPdfDocument::SetTextColor(const unsigned char grayscale)
{
  wxPdfColour tempColor(grayscale);
  m_textColor = tempColor;
  m_colorFlag = (m_fillColor != m_textColor);
}

void
00414 wxPdfDocument::SetTextColor(const wxPdfColour& color)
{
  m_textColor = color;
  m_colorFlag = (m_fillColor != m_textColor);
}

void
00421 wxPdfDocument::SetTextColor(const unsigned char red, const unsigned char green, const unsigned char blue)
{
  SetTextColor(wxColour(red, green, blue));
}

void
00427 wxPdfDocument::SetTextColor(double cyan, double magenta, double yellow, double black)
{
  SetTextColor(wxPdfColour(cyan, magenta, yellow, black));
}

void
00433 wxPdfDocument::SetTextColor(const wxString& name, double tint)
{
  wxPdfSpotColourMap::iterator spotColor = (*m_spotColors).find(name);
  if (spotColor != (*m_spotColors).end())
  {
    wxPdfColour tempColor(*(spotColor->second), tint);
    m_textColor = tempColor;
    m_colorFlag = (m_fillColor != m_textColor);
  }
  else
  {
    wxLogError(_("SetTextColor: Undefined spot color: ") + name);
  }
}
 
const wxPdfColour
00449 wxPdfDocument::GetTextColor()
{
  return wxPdfColour(m_textColor);
}

bool operator==(const wxPdfColour& a, const wxPdfColour& b)
{
  return a.Equals(b);
}

bool operator!=(const wxPdfColour& a, const wxPdfColour& b)
{
  return !a.Equals(b);
}

Generated by  Doxygen 1.6.0   Back to index