Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Author:
4 * Martin Owens <doctormo@geek-2.com>
5 *
6 * Copyright (C) 2023 AUTHORS
7 *
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10#ifndef SEEN_COLORS_COLOR_H
11#define SEEN_COLORS_COLOR_H
12
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "colors/spaces/enum.h"
18#include "utils.h"
19
20namespace Inkscape::Colors {
21namespace Space {
22class AnySpace;
23} // namespace Space
24
25class Color final
26{
27public:
28 Color(std::shared_ptr<Space::AnySpace> space, std::vector<double> colors);
29 Color(Space::Type space_type, std::vector<double> values);
30 explicit Color(uint32_t color, bool alpha = true);
31
32 static std::optional<Color> parse(char const *value);
33 static std::optional<Color> parse(std::string const &value);
34 static std::optional<Color> ifValid(Space::Type space_type, std::vector<double> values);
35
36 bool operator==(Color const &other) const;
37 double operator[](unsigned int index) const { return get(index); }
38
39 std::shared_ptr<Space::AnySpace> const &getSpace() const { return _space; }
40 const std::vector<double> &getValues() const { return _values; }
41 void setValues(std::vector<double> values);
42 size_t size() const { return _values.size(); }
43
44 double get(unsigned int index) const;
45 bool set(unsigned int index, double value);
46 bool set(Color const &other, bool keep_space = true);
47 bool set(std::string const &parsable, bool keep_space = true);
48 bool set(uint32_t rgba, bool opacity = true);
49
50 bool hasOpacity() const;
51 void enableOpacity(bool enabled);
52 unsigned int getOpacityChannel() const;
53 double getOpacity() const;
54 double stealOpacity();
55 bool setOpacity(double opacity);
56 bool addOpacity(double opacity = 1.0) { return setOpacity(opacity * getOpacity()); }
57
58 unsigned int getPin(unsigned int channel) const;
59
60 static constexpr double EPSILON = 1e-4;
61
62 double difference(Color const &other) const;
63 bool isClose(Color const &other, double epsilon = EPSILON) const;
64 bool isSimilar(Color const &other, double epsilon = EPSILON) const;
65
66 bool convert(Color const &other);
67 bool convert(std::shared_ptr<Space::AnySpace> space);
68 bool convert(Space::Type type);
69 std::optional<Color> converted(Color const &other) const;
70 std::optional<Color> converted(std::shared_ptr<Space::AnySpace> to_space) const;
71 std::optional<Color> converted(Space::Type type) const;
72
73 std::string toString(bool opacity = true) const;
74 uint32_t toRGBA(double opacity = 1.0) const;
75 uint32_t toARGB(double opacity = 1.0) const;
76 uint32_t toABGR(double opacity = 1.0) const;
77
78 std::string getName() const { return _name; }
79 void setName(std::string name) { _name = std::move(name); }
80
81 bool isOutOfGamut(std::shared_ptr<Space::AnySpace> other) const;
82 bool isOverInked() const;
83
84 void normalize();
85 Color normalized() const;
86
87 void compose(Color const &other);
88 Color composed(Color const &other) const;
89
90 void average(Color const &other, double pos = 0.5, unsigned int pin = 0);
91 Color averaged(Color const &other, double pos = 0.5) const;
92
93 void invert(unsigned int pin);
95 void jitter(double force, unsigned int pin = 0);
96
97private:
98 std::string _name;
99 std::vector<double> _values;
100 std::shared_ptr<Space::AnySpace> _space;
101
102 template <typename Func>
103 void _color_mutate_inplace(Color const &other, unsigned int pin, Func avgFunc);
104
105 bool _isnear(std::vector<double> const &other, double epsilon = 0.001) const;
106};
107
108class ColorError : public std::exception
109{
110public:
111 ColorError(std::string &&msg)
112 : _msg(msg)
113 {}
114 char const *what() const noexcept override { return _msg.c_str(); }
115
116private:
117 std::string _msg;
118};
119
120} // namespace Inkscape::Colors
121
122#endif // SEEN_COLORS_COLOR_H
char const * what() const noexcept override
Definition color.h:114
ColorError(std::string &&msg)
Definition color.h:111
bool convert(Color const &other)
Convert to the same format as the other color.
Definition color.cpp:145
bool isOutOfGamut(std::shared_ptr< Space::AnySpace > other) const
Return true if this color would be out of gamut when converted to another space.
Definition color.cpp:615
static std::optional< Color > ifValid(Space::Type space_type, std::vector< double > values)
Construct a color from the space type and values, if the values are valid.
Definition color.cpp:331
bool _isnear(std::vector< double > const &other, double epsilon=0.001) const
Returns true if the values are near to the other values.
Definition color.cpp:292
uint32_t toARGB(double opacity=1.0) const
Return the RGBA int32 as an ARGB format number.
Definition color.cpp:125
double stealOpacity()
Get the opacity, and remove it from this color.
Definition color.cpp:417
std::string toString(bool opacity=true) const
Format the color as a css string and return it.
Definition color.cpp:106
std::string _name
Definition color.h:98
double difference(Color const &other) const
Get the mean square difference between this color and another.
Definition color.cpp:566
unsigned int getOpacityChannel() const
Get the opacity channel index.
Definition color.cpp:427
Color normalized() const
Return a normalized copy of this color so the values are within acceptable ranges.
Definition color.cpp:470
bool operator==(Color const &other) const
Return true if the two colors are the same.
Definition color.cpp:73
void _color_mutate_inplace(Color const &other, unsigned int pin, Func avgFunc)
Definition color.cpp:630
size_t size() const
Definition color.h:42
void setValues(std::vector< double > values)
Set the channels directly without checking if the space is correct.
Definition color.cpp:230
bool addOpacity(double opacity=1.0)
Definition color.h:56
uint32_t toRGBA(double opacity=1.0) const
Return an sRGB conversion of the color in RGBA int32 format.
Definition color.cpp:117
bool setOpacity(double opacity)
Set the opacity of this color object.
Definition color.cpp:444
bool isClose(Color const &other, double epsilon=EPSILON) const
Find out if a color is a close match to another color of the same type.
Definition color.cpp:583
bool isSimilar(Color const &other, double epsilon=EPSILON) const
Find out if a color is similar to another color, converting it first if it's a different type.
Definition color.cpp:599
void enableOpacity(bool enabled)
Enables or disables the opacity channel.
Definition color.cpp:385
Color averaged(Color const &other, double pos=0.5) const
Return the average between this and another color.
Definition color.cpp:556
void compose(Color const &other)
Put the other color on top of this color, mixing the two according to the alpha.
Definition color.cpp:517
std::optional< Color > converted(Color const &other) const
Return a copy of this color converted to the same format as the other color.
Definition color.cpp:189
double operator[](unsigned int index) const
Definition color.h:37
void normalize()
Make sure the values for this color are within acceptable ranges.
Definition color.cpp:460
std::vector< double > _values
Definition color.h:99
void jitter(double force, unsigned int pin=0)
Jitter the color for each channel.
Definition color.cpp:499
void setName(std::string name)
Definition color.h:79
static constexpr double EPSILON
Definition color.h:60
unsigned int getPin(unsigned int channel) const
Return the pin number (pow2) of the channel index to pin that channel in a mutation.
Definition color.cpp:436
std::shared_ptr< Space::AnySpace > _space
Definition color.h:100
static std::optional< Color > parse(char const *value)
Create an optional color if value is valid.
Definition color.cpp:304
bool hasOpacity() const
Returns true if there is an opacity channel in this color.
Definition color.cpp:398
double getOpacity() const
Get the opacity in this color, if it's stored.
Definition color.cpp:407
Color composed(Color const &other) const
Return the composition of this color, plus the other color on top.
Definition color.cpp:530
void average(Color const &other, double pos=0.5, unsigned int pin=0)
Definition color.cpp:544
std::shared_ptr< Space::AnySpace > const & getSpace() const
Definition color.h:39
std::string getName() const
Definition color.h:78
const std::vector< double > & getValues() const
Definition color.h:40
bool isOverInked() const
Return true if this color would be considered over-inked.
Definition color.cpp:623
uint32_t toABGR(double opacity=1.0) const
Return the RGBA int32 as an ABGR format color.
Definition color.cpp:134
double get(unsigned int index) const
Get a single channel from this color.
Definition color.cpp:87
Glib::ustring msg
A set of useful color modifying functions which do not fit as generic methods on the color class itse...
Definition profile.cpp:24
int index
Glib::ustring name
Definition toolbars.cpp:55