"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/stylecolour.cpp" (12 May 2020, 4386 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /***************************************************************************
    2                           stylecolour.cpp  -  description
    3                              -------------------
    4     begin                : Die Nov 5 2002
    5     copyright            : (C) 2002 by Andre Simon
    6     email                : a.simon@mailbox.org
    7  ***************************************************************************/
    8 
    9 
   10 /*
   11 This file is part of Highlight.
   12 
   13 Highlight is free software: you can redistribute it and/or modify
   14 it under the terms of the GNU General Public License as published by
   15 the Free Software Foundation, either version 3 of the License, or
   16 (at your option) any later version.
   17 
   18 Highlight is distributed in the hope that it will be useful,
   19 but WITHOUT ANY WARRANTY; without even the implied warranty of
   20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   21 GNU General Public License for more details.
   22 
   23 You should have received a copy of the GNU General Public License
   24 along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   25 */
   26 
   27 
   28 #include "stylecolour.h"
   29 #include "stringtools.h"
   30 
   31 
   32 #include <iostream>
   33 #include <sstream>
   34 #include <cmath>
   35 
   36 using std::string;
   37 
   38 namespace highlight
   39 {
   40 
   41 Colour::Colour ( const string & red, const string & green, const string & blue )
   42 {
   43     rgb.iRed = rgb.iGreen = rgb.iBlue = 0;
   44 
   45     ostringstream rgbStream;
   46     rgbStream << red << " " << green << " " << blue;
   47     setRGB ( rgbStream.str() );
   48 }
   49 
   50 Colour::Colour()
   51 {
   52     rgb.iRed = rgb.iGreen = rgb.iBlue = 0;
   53 }
   54 
   55 Colour::Colour ( const string & colourString )
   56 {
   57     rgb.iRed = rgb.iGreen = rgb.iBlue = 0;
   58 
   59     setRGB ( colourString );
   60 }
   61 
   62 void Colour::setRGB ( const string & colourString )
   63 {
   64     if ( colourString.empty() ) return;
   65 
   66     istringstream valueStream ( colourString.c_str() );
   67     string r, g, b;
   68     char c='\0';
   69     valueStream >> c;
   70 
   71     if ( c=='#' ) {
   72         string htmlNotation;
   73         valueStream >> htmlNotation;
   74         if ( htmlNotation.size() < 6 ) return;
   75         r = htmlNotation.substr ( 0, 2 );
   76         g = htmlNotation.substr ( 2, 2 );
   77         b = htmlNotation.substr ( 4, 2 );
   78     } else {
   79         valueStream.putback ( c );
   80         valueStream >> r;
   81         valueStream >> g;
   82         valueStream >> b;
   83     }
   84 
   85     StringTools::str2num<int> ( rgb.iRed,   r, std::hex );
   86     StringTools::str2num<int> ( rgb.iGreen, g, std::hex );
   87     StringTools::str2num<int> ( rgb.iBlue,  b, std::hex );
   88 }
   89 
   90 void Colour::setRed ( const string & red )
   91 {
   92     StringTools::str2num<int> ( rgb.iRed, red, std::hex );
   93 }
   94 
   95 void Colour::setGreen ( const string & green )
   96 {
   97     StringTools::str2num<int> ( rgb.iGreen, green, std::hex );
   98 }
   99 
  100 void Colour::setBlue ( const string & blue )
  101 {
  102     StringTools::str2num<int> ( rgb.iBlue, blue, std::hex );
  103 }
  104 
  105 int Colour::getRed () const
  106 {
  107     return rgb.iRed;
  108 }
  109 
  110 int Colour::getGreen () const
  111 {
  112     return rgb.iGreen;
  113 }
  114 
  115 int Colour::getBlue () const
  116 {
  117     return rgb.iBlue;
  118 }
  119 
  120 string Colour::getRed ( OutputType type ) const
  121 {
  122     switch ( type ) {
  123     case RTF:
  124         return int2str ( rgb.iRed, std::dec );
  125     case LATEX:
  126         return float2str ( ( float ) rgb.iRed / 255 );
  127     case TEX:
  128         return float2str ( 1 - ( float ) rgb.iRed / 255 );
  129     default:
  130         return int2str ( rgb.iRed, std::hex );
  131     }
  132 }
  133 
  134 string Colour::getGreen ( OutputType type ) const
  135 {
  136     switch ( type ) {
  137     case RTF:
  138         return int2str ( rgb.iGreen, std::dec );
  139     case LATEX:
  140         return float2str ( ( float ) rgb.iGreen / 255 );
  141     case TEX:
  142         return float2str ( 1 - ( float ) rgb.iGreen / 255 );
  143     default:
  144         return int2str ( rgb.iGreen, std::hex );
  145     }
  146 }
  147 
  148 string Colour::getBlue ( OutputType type ) const
  149 {
  150     switch ( type ) {
  151     case RTF:
  152         return int2str ( rgb.iBlue, std::dec );
  153     case LATEX:
  154         return float2str ( ( float ) rgb.iBlue / 255 );
  155     case TEX:
  156         return float2str ( 1 - ( float ) rgb.iBlue / 255 );
  157     default:
  158         return int2str ( rgb.iBlue, std::hex );
  159     }
  160 }
  161 
  162 
  163 string Colour::int2str ( const int num, std::ios_base& ( *f ) ( std::ios_base& ) ) const
  164 {
  165     std::ostringstream outStream;
  166     outStream.width ( 2 );
  167     outStream.fill ( '0' );
  168     outStream << f << num;
  169 
  170     return outStream.str();
  171 }
  172 
  173 string Colour::float2str ( const double num ) const
  174 {
  175     std::ostringstream outStream;
  176     outStream << ( floor ( num * 100 + .5 ) / 100 );
  177 
  178     return outStream.str();
  179 }
  180 
  181 }
  182