1 // Aseprite
2 // Copyright (C) 2001-2018  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_COLOR_H_INCLUDED
8 #define APP_COLOR_H_INCLUDED
9 #pragma once
10 
11 #include "doc/color.h"
12 #include "doc/pixel_format.h"
13 
14 #include <string>
15 
16 namespace doc {
17   class Image;
18   class Layer;
19 }
20 
21 namespace app {
22 
23   using namespace doc;
24 
25   class Color {
26   public:
27     enum Type {
28       MaskType,
29       RgbType,
30       HsvType,
31       HslType,
32       GrayType,
33       IndexType,
34     };
35 
36     enum HumanReadableString {
37       ShortHumanReadableString,
38       LongHumanReadableString
39     };
40 
41     // Default ctor is mask color
Color()42     Color() : m_type(MaskType) { }
43 
44     static Color fromMask();
45     static Color fromRgb(int r, int g, int b, int a = 255);
46     static Color fromHsv(double h, double s, double v, int a = 255); // h=[0,360], s=[0,1], v=[0,1]
47     static Color fromHsl(double h, double s, double l, int a = 255); // h=[0,360], s=[0,1], v=[0,1]
48     static Color fromGray(int g, int a = 255);
49     static Color fromIndex(int index);
50 
51     static Color fromImage(PixelFormat pixelFormat, color_t c);
52     static Color fromImageGetPixel(Image* image, int x, int y);
53     static Color fromString(const std::string& str);
54 
55     Color toRgb() const;
56     std::string toString() const;
57     std::string toHumanReadableString(PixelFormat format, HumanReadableString type) const;
58 
59     bool operator==(const Color& other) const;
60     bool operator!=(const Color& other) const {
61       return !operator==(other);
62     }
63 
getType()64     Type getType() const {
65       return m_type;
66     }
67 
68     bool isValid() const;
69 
70     // Getters
71     int getRed() const;
72     int getGreen() const;
73     int getBlue() const;
74     double getHsvHue() const;
75     double getHsvSaturation() const;
76     double getHsvValue() const;
77     double getHslHue() const;
78     double getHslSaturation() const;
79     double getHslLightness() const;
80     int getGray() const;
81     int getIndex() const;
82     int getAlpha() const;
83 
84     // Setters
85     void setAlpha(int alpha);
86 
87   private:
Color(Type type)88     Color(Type type) : m_type(type) { }
89 
90     // Color type
91     Type m_type;
92 
93     // Color value
94     union {
95       struct {
96         int r, g, b, a;
97       } rgb;
98       struct {
99         double h, s, v;
100         int a;
101       } hsv;
102       struct {
103         double h, s, l;
104         int a;
105       } hsl;
106       struct {
107         int g, a;
108       } gray;
109       int index;
110     } m_value;
111   };
112 
113 } // namespace app
114 
115 #endif
116