LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
color.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <iostream>
6 
7 namespace lc {
8 
13  class Color {
14  public:
15  Color() : _r(0.), _g(0.), _b(0.), _a(1.) {}
16  Color(int r, int g, int b, int a = 0xff) : _r(std::min(std::max(0, r), 0xff) / 255.) , _g(std::min(std::max(0, g), 0xff) / 255.), _b(std::min(std::max(0, b), 0xff) / 255.), _a(std::min(std::max(0, a), 0xff) / 255.) {
17  }
18  Color(double r, double g, double b, double a = 1.) : _r(std::min(std::max(0., r), 1.)), _g(std::min(std::max(0., g), 1.)), _b(std::min(std::max(0., b), 1.)), _a(std::min(std::max(0., a), 1.)) {
19  }
20  Color(const Color& other) : _r(other._r), _g(other._g), _b(other._b), _a(other._a) {}
21 
22  Color& operator = (const Color& other) {
23  if (this != &other) {
24  _r = other._r;
25  _g = other._g;
26  _b = other._b;
27  _a = other._a;
28  }
29 
30  return *this;
31  }
32 
33 
35  return _r == b._r && _b == b._b && _g == b._g && _a == b._a;
36  }
37 
38 
39  virtual ~Color() {}
40 
41  inline double red() const {
42  return _r;
43  }
44  inline double green() const {
45  return _g;
46  }
47  inline double blue() const {
48  return _b;
49  }
50  inline double alpha() const {
51  return _a;
52  }
53 
54  inline unsigned char redI() const {
55  return std::round(_r * 255);
56  }
57  inline unsigned char greenI() const {
58  return std::round(_g * 255);
59  }
60  inline unsigned char blueI() const {
61  return std::round(_b * 255);
62  }
63  inline unsigned char alphaI() const {
64  return std::round(_a * 255);
65  }
66 
67  friend std::ostream& operator<<(std::ostream& os, const Color& color) {
68  os << "Color(red=" << color._r << " green=" << color._g << " blue=" << color._b << " ahlpa=" << color._a << ")";
69  return os;
70  }
71 
72  private:
73  double _r;
74  double _g;
75  double _b;
76  double _a;
77  };
78 
79 
80 }
double alpha() const
Definition: color.h:50
double blue() const
Definition: color.h:47
unsigned char alphaI() const
Definition: color.h:63
double _g
Definition: color.h:74
unsigned char redI() const
Definition: color.h:54
double green() const
Definition: color.h:44
bool operator==(lc::Color b)
Definition: color.h:34
Color & operator=(const Color &other)
Definition: color.h:22
double red() const
Definition: color.h:41
Color(double r, double g, double b, double a=1.)
Definition: color.h:18
Color(int r, int g, int b, int a=0xff)
Definition: color.h:16
Definition: cadentity.h:12
virtual ~Color()
Definition: color.h:39
unsigned char blueI() const
Definition: color.h:60
unsigned char greenI() const
Definition: color.h:57
friend std::ostream & operator<<(std::ostream &os, const Color &color)
Definition: color.h:67
double _a
Definition: color.h:76
double _r
Definition: color.h:73
double _b
Definition: color.h:75
Color(const Color &other)
Definition: color.h:20
Color()
Definition: color.h:15