1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #ifndef SEEN_SP_COLOR_H
3 #define SEEN_SP_COLOR_H
4 
5 /*
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *   Jon A. Cruz <jon@joncruz.org>
10  *
11  * Copyright (C) 2001-2002 Lauris Kaplinski
12  * Copyright (C) 2001 Ximian, Inc.
13  *
14  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15  */
16 
17 #include <string>
18 typedef unsigned int guint32; // uint is guaranteed to hold up to 2^32 − 1
19 
20 /* Useful composition macros */
21 
22 #define SP_RGBA32_R_U(v) (((v) >> 24) & 0xff)
23 #define SP_RGBA32_G_U(v) (((v) >> 16) & 0xff)
24 #define SP_RGBA32_B_U(v) (((v) >> 8) & 0xff)
25 #define SP_RGBA32_A_U(v) ((v) & 0xff)
26 #define SP_COLOR_U_TO_F(v) ((v) / 255.0)
27 #define SP_COLOR_F_TO_U(v) ((unsigned int) ((v) * 255. + .5))
28 #define SP_RGBA32_R_F(v) SP_COLOR_U_TO_F (SP_RGBA32_R_U (v))
29 #define SP_RGBA32_G_F(v) SP_COLOR_U_TO_F (SP_RGBA32_G_U (v))
30 #define SP_RGBA32_B_F(v) SP_COLOR_U_TO_F (SP_RGBA32_B_U (v))
31 #define SP_RGBA32_A_F(v) SP_COLOR_U_TO_F (SP_RGBA32_A_U (v))
32 #define SP_RGBA32_U_COMPOSE(r,g,b,a) ((((r) & 0xff) << 24) | (((g) & 0xff) << 16) | (((b) & 0xff) << 8) | ((a) & 0xff))
33 #define SP_RGBA32_F_COMPOSE(r,g,b,a) SP_RGBA32_U_COMPOSE (SP_COLOR_F_TO_U (r), SP_COLOR_F_TO_U (g), SP_COLOR_F_TO_U (b), SP_COLOR_F_TO_U (a))
34 #define SP_RGBA32_C_COMPOSE(c,o) SP_RGBA32_U_COMPOSE(SP_RGBA32_R_U(c),SP_RGBA32_G_U(c),SP_RGBA32_B_U(c),SP_COLOR_F_TO_U(o))
35 
36 struct SVGICCColor;
37 
38 /**
39  * An RGB color with optional icc-color part
40  */
41 struct SPColor {
42     SPColor();
43     SPColor( SPColor const& other );
44     SPColor( float r, float g, float b );
45     SPColor( guint32 value );
46     virtual ~SPColor();
47 
48     SPColor& operator= (SPColor const& other);
49 
50     bool operator == ( SPColor const& other ) const;
51     bool operator != ( SPColor const& other ) const { return !(*this == other); };
52     bool isClose( SPColor const& other, float epsilon ) const;
53 
54     void set( float r, float g, float b );
55     void set( guint32 value );
56 
57     guint32 toRGBA32( int alpha ) const;
58     guint32 toRGBA32( double alpha ) const;
59 
60     std::string toString() const;
61 
62     SVGICCColor* icc;
63     union {
64         float c[3];
65     } v;
66 
67     guint32 get_rgba32_ualpha (guint32 alpha) const;
68     guint32 get_rgba32_falpha (float alpha) const;
69 
70     void get_rgb_floatv (float *rgb) const;
71     void get_cmyk_floatv (float *cmyk) const;
72 
73     /* Plain mode helpers */
74 
75     static void rgb_to_hsv_floatv (float *hsv, float r, float g, float b);
76     static void hsv_to_rgb_floatv (float *rgb, float h, float s, float v);
77 
78     static void rgb_to_hsl_floatv (float *hsl, float r, float g, float b);
79     static void hsl_to_rgb_floatv (float *rgb, float h, float s, float l);
80 
81     static void rgb_to_cmyk_floatv (float *cmyk, float r, float g, float b);
82     static void cmyk_to_rgb_floatv (float *rgb, float c, float m, float y, float k);
83 };
84 
85 
86 #endif // SEEN_SP_COLOR_H
87