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