Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
cf858b16fd6f2f88c9cf9f85953726d333ef3f3d
[xestiacalendar/.git] / source / common / colour.h
1 // colour.h - Colour Structures
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include <string>
20 #include <sstream>
21 #include "text.h"
23 #ifndef __COMMON_COLOUR_H__
24 #define __COMMON_COLOUR_H__
26 using namespace std;
28 struct Colour{
29         
30         int red = 0;
31         int blue = 0;
32         int green = 0;
33         int alpha = 255;
35         Colour& operator= (string const &arg){
37                 // Check if the size of the string is 9.
38                 // Return if not.
39                 
40                 if (arg.size() != 9){
41                         
42                         return *this;
43                         
44                 }
45                 
46                 // Check if the first character has a hash in.
47                 // Return if not.
48                 
49                 if (arg.substr(0,1) != "#"){
50                         
51                         return *this;
52                         
53                 }
54                 
55                 // Check each character (1-8) and make sure
56                 // it is a number or a letter between A and F.
57                 
58                 for (int characterSeek = 1; characterSeek < 9; characterSeek++){
59                         
60                         int characterASCII = 0;
61                         
62                         characterASCII = arg[characterSeek];
63                         
64                         if ((characterASCII < 48 && characterASCII > 57) || (characterASCII < 65 && characterASCII > 70)){
65                                 return *this;
66                         }
67                         
68                 }
69                 
70                 istringstream hexNumber;
71                 
72                 // Set the red value.
74                 hexNumber.str(arg.substr(1,2));
75                 hexNumber >> hex >> this->red;
76                 
77                 // Set the green value.
79                 hexNumber.clear();
80                 hexNumber.str(arg.substr(3,2));
81                 hexNumber >> hex >> this->green;
82                 
83                 // Set the blue value.
85                 hexNumber.clear();
86                 hexNumber.str(arg.substr(5,2));
87                 hexNumber >> hex >> this->blue;
88                 
89                 // Set the alpha value.
90                 
91                 hexNumber.clear();
92                 hexNumber.str(arg.substr(7,2));
93                 hexNumber >> hex >> this->alpha;
94                 
95                 return *this;
96                 
97         }
98         
99         operator string() {
100                 
101                 string ColourOut;
102                 string ColourOutHex;
103         
104                 ColourOut = "#";
105                 
106                 // Convert the red value.
107                 
108                 if (red > 255){
109                         ColourOut += "FF";
110                 } else if (red < 0) {
111                         ColourOut += "00";                      
112                 } else {
113                         IntToHex(&red, &ColourOutHex, 2);
114                         ColourOut += ColourOutHex;
115                 }
116                 
117                 // Convert the green value.
119                 if (green > 255){
120                         ColourOut += "FF";
121                 } else if (green < 0){
122                         ColourOut += "00";                      
123                 } else {
124                         IntToHex(&green, &ColourOutHex, 2);
125                         ColourOut += ColourOutHex;                      
126                 }
127                 
128                 // Convert the blue value.
130                 if (blue > 255){
131                         ColourOut += "FF";
132                 } else if (blue < 0) {
133                         ColourOut += "00";                      
134                 } else {
135                         IntToHex(&blue, &ColourOutHex, 2);
136                         ColourOut += ColourOutHex;
137                 }
138                 
139                 // Convert the alpha value.
140                 
141                 if (alpha > 255){
142                         ColourOut += "FF";
143                 } else if (alpha < 0){
144                         ColourOut += "00";
145                 } else {
146                         IntToHex(&alpha, &ColourOutHex, 2);
147                         ColourOut += ColourOutHex;
148                 }
149                 
150                 return ColourOut;
151                 
152         }
153         
154 };
156 #endif
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy