ClanLib  2.3.7
color.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Harry Storbacka
28 ** Mark Page
29 */
30 
33 
34 #pragma once
35 
36 #include "../api_display.h"
37 #include "../../Core/Text/string_types.h"
38 #include <vector>
39 
40 class CL_PixelFormat;
41 class CL_Colord;
42 class CL_Colorf;
43 
48 {
51 
52 public:
55  CL_Color() : color(0) { return; }
56 
57  explicit CL_Color(const CL_Colord&);
58  explicit CL_Color(const CL_Colorf&);
59 
69  CL_Color(unsigned int red, unsigned int green, unsigned int blue, unsigned int alpha = 255)
70  : color((alpha<<24) | (red<<16) | (green<<8) | blue) { return; }
71 
78  CL_Color(const CL_StringRef &hexstr);
79 
83 
84 public:
86  unsigned int get_alpha() const { return (color >> 24) & 0xff; }
87 
89  unsigned int get_red() const { return (color >> 16) & 0xff; }
90 
92  unsigned int get_green() const { return (color >> 8) & 0xff; }
93 
95  unsigned int get_blue() const { return color & 0xff; }
96 
98  double get_alpha_d() const { return float(get_alpha()) / 255.0; }
99 
101  double get_red_d() const { return float(get_red()) / 255.0; }
102 
104  double get_green_d() const { return float(get_green()) / 255.0; }
105 
107  double get_blue_d() const { return float(get_blue()) / 255.0; }
108 
110  float get_alpha_f() const { return float(get_alpha()) / 255.0f; }
111 
113  float get_red_f() const { return float(get_red()) / 255.0f; }
114 
116  float get_green_f() const { return float(get_green()) / 255.0f; }
117 
119  float get_blue_f() const { return float(get_blue()) / 255.0f; }
120 
122  unsigned int color;
123 
124 // Operations:
125 public:
127  bool operator==(const CL_Color &c) const
128  { return (color == c.color); }
129 
131  bool operator!=(const CL_Color &c) const
132  { return (color != c.color); }
133 
137 
138 public:
141 
144 
146  static CL_Color aqua;
147 
150 
152  static CL_Color azure;
153 
155  static CL_Color beige;
156 
158  static CL_Color bisque;
159 
161  static CL_Color black;
162 
165 
167  static CL_Color blue;
168 
171 
173  static CL_Color brown;
174 
177 
180 
183 
186 
188  static CL_Color coral;
189 
192 
195 
198 
200  static CL_Color cyan;
201 
204 
207 
210 
213 
216 
219 
222 
225 
228 
231 
234 
237 
240 
243 
246 
249 
252 
255 
258 
261 
264 
267 
270 
273 
276 
279 
282 
285 
288 
291 
293  static CL_Color gold;
294 
297 
299  static CL_Color gray;
300 
302  static CL_Color grey;
303 
305  static CL_Color green;
306 
309 
312 
315 
318 
320  static CL_Color indigo;
321 
323  static CL_Color ivory;
324 
326  static CL_Color khaki;
327 
330 
333 
336 
339 
342 
345 
348 
351 
354 
357 
360 
363 
366 
369 
372 
375 
378 
381 
384 
386  static CL_Color lime;
387 
390 
392  static CL_Color linen;
393 
396 
398  static CL_Color maroon;
399 
402 
405 
408 
411 
414 
417 
420 
423 
426 
429 
432 
435 
438 
441 
443  static CL_Color navy;
444 
447 
449  static CL_Color olive;
450 
453 
455  static CL_Color orange;
456 
459 
461  static CL_Color orchid;
462 
465 
468 
471 
474 
477 
480 
482  static CL_Color peru;
483 
485  static CL_Color pink;
486 
488  static CL_Color plum;
489 
492 
494  static CL_Color purple;
495 
497  static CL_Color red;
498 
501 
504 
507 
509  static CL_Color salmon;
510 
513 
516 
519 
521  static CL_Color sienna;
522 
524  static CL_Color silver;
525 
528 
531 
534 
537 
539  static CL_Color snow;
540 
543 
546 
548  static CL_Color tan;
549 
551  static CL_Color teal;
552 
555 
557  static CL_Color tomato;
558 
561 
563  static CL_Color violet;
564 
566  static CL_Color wheat;
567 
569  static CL_Color white;
570 
573 
575  static CL_Color yellow;
576 
579 
582 
583  static CL_Color gray10;
584  static CL_Color gray20;
585  static CL_Color gray30;
586  static CL_Color gray40;
587  static CL_Color gray50;
588  static CL_Color gray60;
589  static CL_Color gray70;
590  static CL_Color gray80;
591  static CL_Color gray90;
592 
598  static CL_Color find_color(const CL_StringRef &name);
599 
601  static std::vector<CL_String8> &get_names();
602 
606 
607 public:
609  void set_alpha(unsigned int value) { color = (color & 0x00ffffff) | (value << 24); }
610 
612  void set_red(unsigned int value) { color = (color & 0xff00ffff) | (value << 16); }
613 
615  void set_green(unsigned int value) { color = (color & 0xffff00ff) | (value << 8); }
616 
618  void set_blue(unsigned int value) { color = (color & 0xffffff00) | value; }
619 
621  void set_alpha_d(float value) { set_alpha((unsigned int) (value*255)); }
622 
624  void set_red_d(float value) { set_red((unsigned int) (value*255)); }
625 
627  void set_green_d(float value) { set_green((unsigned int) (value*255)); }
628 
630  void set_blue_d(float value) { set_blue((unsigned int) (value*255)); }
631 
633  void set_color(unsigned int red, unsigned int green, unsigned int blue, unsigned int alpha = 255)
634  { color = (alpha<<24) | (red<<16) | (green<<8) | blue; }
635 
637  void set_color_d(float red, float green, float blue, float alpha = 1.0)
638  {
639  color =
640  (((unsigned int) (alpha*255))<<24) |
641  (((unsigned int) (red*255))<<16) |
642  (((unsigned int) (green*255))<<8) |
643  ((unsigned int) (blue*255));
644  }
645 
647  //unsigned int to_pixelformat(const CL_PixelFormat &pf) const;
648 
650  //static CL_Color from_pixelformat(unsigned int raw_color, const CL_PixelFormat &pf);
652 };
653 
658 {
661 
662 public:
663 
665  CL_Colord() : r(0.0), g(0.0), b(0.0), a(0.0)
666  {
667  }
668 
678  CL_Colord(double r, double g, double b, double a = 1.0)
679  : r(r), g(g), b(b), a(a)
680  {
681  }
682 
689  CL_Colord(const double *array_rgba)
690  : r(array_rgba[0]), g(array_rgba[1]), b(array_rgba[2]), a(array_rgba[3])
691  {
692  }
693 
703  CL_Colord(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
704  : r(r/255.0),
705  g(g/255.0),
706  b(b/255.0),
707  a(a/255.0)
708  {
709  }
710 
711  explicit CL_Colord(const CL_Color& color)
712  : r(color.get_red()/255.0),
713  g(color.get_green()/255.0),
714  b(color.get_blue()/255.0),
715  a(color.get_alpha()/255.0)
716  {
717  }
718 
725  CL_Colord(const CL_StringRef &hexstr)
726  {
727  *this = (CL_Colord)CL_Color::find_color(hexstr);
728  }
729 
733 
734 public:
735  double r, g, b, a;
736 
740  double get_red() const { return r; }
741 
745  double get_green() const { return g; }
746 
750  double get_blue() const { return b; }
751 
755  double get_alpha() const { return a; }
756 
760 
761 public:
763  void normalize()
764  {
765  r = (r < 0.0) ? 0.0 : ((r > 1.0) ? 1.0 : r);
766  g = (g < 0.0) ? 0.0 : ((g > 1.0) ? 1.0 : g);
767  b = (b < 0.0) ? 0.0 : ((b > 1.0) ? 1.0 : b);
768  a = (a < 0.0) ? 0.0 : ((a > 1.0) ? 1.0 : a);
769  }
770 
772  void set_alpha(double value) { a = value; }
773 
775  void set_red(double value) { r = value; }
776 
778  void set_green(double value) { g= value; }
779 
781  void set_blue(double value) { b = value; }
782 
784  bool operator==(const CL_Colord &c) const
785  {
786  return (r == c.r) && (g == c.g) && (b == c.b) && (a == c.a);
787  }
788 
790  bool operator!=(const CL_Colord &c) const
791  {
792  return (r != c.r) || (g != c.g) || (b != c.b) || (a != c.a);
793  }
794 
796  operator CL_Color() const
797  {
798  return CL_Color(*this);
799  }
800 
804 
805 public:
808 
811 
813  static CL_Colord aqua;
814 
817 
819  static CL_Colord azure;
820 
822  static CL_Colord beige;
823 
826 
828  static CL_Colord black;
829 
832 
834  static CL_Colord blue;
835 
838 
840  static CL_Colord brown;
841 
844 
847 
850 
853 
855  static CL_Colord coral;
856 
859 
862 
865 
867  static CL_Colord cyan;
868 
871 
874 
877 
880 
883 
886 
889 
892 
895 
898 
901 
904 
907 
910 
913 
916 
919 
922 
925 
928 
931 
934 
937 
940 
943 
946 
949 
952 
955 
958 
960  static CL_Colord gold;
961 
964 
966  static CL_Colord gray;
967 
969  static CL_Colord grey;
970 
972  static CL_Colord green;
973 
976 
979 
982 
985 
988 
990  static CL_Colord ivory;
991 
993  static CL_Colord khaki;
994 
997 
1000 
1003 
1006 
1009 
1012 
1015 
1018 
1021 
1024 
1027 
1030 
1033 
1036 
1039 
1042 
1045 
1048 
1051 
1053  static CL_Colord lime;
1054 
1057 
1060 
1063 
1066 
1069 
1072 
1075 
1078 
1081 
1084 
1087 
1090 
1093 
1096 
1099 
1102 
1105 
1108 
1110  static CL_Colord navy;
1111 
1114 
1117 
1120 
1123 
1126 
1129 
1132 
1135 
1138 
1141 
1144 
1147 
1149  static CL_Colord peru;
1150 
1152  static CL_Colord pink;
1153 
1155  static CL_Colord plum;
1156 
1159 
1162 
1164  static CL_Colord red;
1165 
1168 
1171 
1174 
1177 
1180 
1183 
1186 
1189 
1192 
1195 
1198 
1201 
1204 
1206  static CL_Colord snow;
1207 
1210 
1213 
1215  static CL_Colord tan;
1216 
1218  static CL_Colord teal;
1219 
1222 
1225 
1228 
1231 
1234 
1237 
1240 
1243 
1246 
1249 
1260 };
1261 
1266 {
1269 
1270 public:
1272  CL_Colorf() : r(0.0f), g(0.0f), b(0.0f), a(0.0f)
1273  {
1274  }
1275 
1285  CL_Colorf(float r, float g, float b, float a = 1.0f)
1286  : r(r), g(g), b(b), a(a)
1287  {
1288  }
1289 
1296  CL_Colorf(const float *array_rgba)
1297  : r(array_rgba[0]), g(array_rgba[1]), b(array_rgba[2]), a(array_rgba[3])
1298  {
1299  }
1300 
1310  CL_Colorf(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
1311  : r(r/255.0f),
1312  g(g/255.0f),
1313  b(b/255.0f),
1314  a(a/255.0f)
1315  {
1316  }
1317 
1327  CL_Colorf(int r, int g, int b, int a=255)
1328  : r(r/255.0f),
1329  g(g/255.0f),
1330  b(b/255.0f),
1331  a(a/255.0f)
1332  {
1333  }
1334 
1335  explicit CL_Colorf(const CL_Color& color)
1336  : r(color.get_red()/255.0f),
1337  g(color.get_green()/255.0f),
1338  b(color.get_blue()/255.0f),
1339  a(color.get_alpha()/255.0f)
1340  {
1341  }
1342 
1349  CL_Colorf(const CL_StringRef &hexstr)
1350  {
1351  *this = (CL_Colorf)CL_Color::find_color(hexstr);
1352  }
1353 
1357 
1358 public:
1359  float r, g, b, a;
1360 
1364  float get_red() const { return r; }
1365 
1369  float get_green() const { return g; }
1370 
1374  float get_blue() const { return b; }
1375 
1379  float get_alpha() const { return a; }
1380 
1384 
1385 public:
1387  void normalize()
1388  {
1389  r = (r < 0.0f) ? 0.0f : ((r > 1.0f) ? 1.0f : r);
1390  g = (g < 0.0f) ? 0.0f : ((g > 1.0f) ? 1.0f : g);
1391  b = (b < 0.0f) ? 0.0f : ((b > 1.0f) ? 1.0f : b);
1392  a = (a < 0.0f) ? 0.0f : ((a > 1.0f) ? 1.0f : a);
1393  }
1394 
1396  void set_alpha(float value) { a = value; }
1397 
1399  void set_red(float value) { r = value; }
1400 
1402  void set_green(float value) { g= value; }
1403 
1405  void set_blue(float value) { b = value; }
1406 
1408  bool operator==(const CL_Colorf &c) const
1409  {
1410  return (r == c.r) && (g == c.g) && (b == c.b) && (a == c.a);
1411  }
1412 
1414  bool operator!=(const CL_Colorf &c) const
1415  {
1416  return (r != c.r) || (g != c.g) || (b != c.b) || (a != c.a);
1417  }
1418 
1420  operator CL_Color() const
1421  {
1422  return CL_Color(*this);
1423  }
1424 
1428 
1429 public:
1432 
1435 
1437  static CL_Colorf aqua;
1438 
1441 
1444 
1447 
1450 
1453 
1456 
1458  static CL_Colorf blue;
1459 
1462 
1465 
1468 
1471 
1474 
1477 
1480 
1483 
1486 
1489 
1491  static CL_Colorf cyan;
1492 
1495 
1498 
1501 
1504 
1507 
1510 
1513 
1516 
1519 
1522 
1525 
1528 
1531 
1534 
1537 
1540 
1543 
1546 
1549 
1552 
1555 
1558 
1561 
1564 
1567 
1570 
1573 
1576 
1579 
1582 
1584  static CL_Colorf gold;
1585 
1588 
1590  static CL_Colorf gray;
1591 
1593  static CL_Colorf grey;
1594 
1597 
1600 
1603 
1606 
1609 
1612 
1615 
1618 
1621 
1624 
1627 
1630 
1633 
1636 
1639 
1642 
1645 
1648 
1651 
1654 
1657 
1660 
1663 
1666 
1669 
1672 
1675 
1677  static CL_Colorf lime;
1678 
1681 
1684 
1687 
1690 
1693 
1696 
1699 
1702 
1705 
1708 
1711 
1714 
1717 
1720 
1723 
1726 
1729 
1732 
1734  static CL_Colorf navy;
1735 
1738 
1741 
1744 
1747 
1750 
1753 
1756 
1759 
1762 
1765 
1768 
1771 
1773  static CL_Colorf peru;
1774 
1776  static CL_Colorf pink;
1777 
1779  static CL_Colorf plum;
1780 
1783 
1786 
1788  static CL_Colorf red;
1789 
1792 
1795 
1798 
1801 
1804 
1807 
1810 
1813 
1816 
1819 
1822 
1825 
1828 
1830  static CL_Colorf snow;
1831 
1834 
1837 
1839  static CL_Colorf tan;
1840 
1842  static CL_Colorf teal;
1843 
1846 
1849 
1852 
1855 
1858 
1861 
1864 
1867 
1870 
1873 
1884 };
1885