Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
rgb.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
4 * Authors: see git history
5 *
6 * Copyright (C) 2023 Authors
7 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
8 */
9
10#include "rgb.h"
11
12#include <sstream>
13
14#include "colors/cms/profile.h"
15#include "colors/color.h"
16#include "colors/utils.h"
17
19
23std::shared_ptr<Inkscape::Colors::CMS::Profile> const RGB::getProfile() const
24{
25 static std::shared_ptr<Colors::CMS::Profile> srgb_profile;
26 if (!srgb_profile) {
28 }
29 return srgb_profile;
30}
31
32RGB::RGB(Type type, int components, std::string name, std::string shortName, std::string icon, bool spaceIsUnbounded):
33 AnySpace(type, components, std::move(name), std::move(shortName), std::move(icon), spaceIsUnbounded) {
34}
35
42std::string RGB::toString(std::vector<double> const &values, bool opacity) const
43{
44 return rgba_to_hex(toRGBA(values), values.size() == 4 && opacity);
45}
46
50uint32_t RGB::toRGBA(std::vector<double> const &values, double opacity) const
51{
52 if (getType() != Type::RGB) {
53 std::vector<double> copy = values;
54 spaceToProfile(copy);
55 return _to_rgba(copy, opacity);
56 }
57 return _to_rgba(values, opacity);
58}
59
60uint32_t RGB::_to_rgba(std::vector<double> const &values, double opacity) const
61{
62 switch (values.size()) {
63 case 3:
64 return SP_RGBA32_F_COMPOSE(values[0], values[1], values[2], opacity);
65 case 4:
66 return SP_RGBA32_F_COMPOSE(values[0], values[1], values[2], opacity * values[3]);
67 default:
68 throw ColorError("Color values should be size 3 for RGB or 4 for RGBA.");
69 }
70 return 0x0; // transparent black
71}
72
73bool RGB::Parser::parse(std::istringstream &ss, std::vector<double> &output) const
74{
75 bool end = false;
76 return append_css_value(ss, output, end, ',', 255) // Red
77 && append_css_value(ss, output, end, ',', 255) // Green
78 && append_css_value(ss, output, end, !_alpha ? '/' : ',', 255) // Blue
79 && (append_css_value(ss, output, end) || !_alpha) // Opacity
80 && end;
81}
82
83} // namespace Inkscape::Colors::Space
static std::shared_ptr< Profile > create_srgb()
Construct the default lcms sRGB color profile and return.
Definition profile.cpp:70
static bool append_css_value(std::istringstream &ss, std::vector< double > &output, bool &end, char const sep=0x0, double scale=1.0)
Parse a CSS color number and format it according to it's unit.
Definition parser.cpp:324
std::shared_ptr< Colors::CMS::Profile > srgb_profile
Definition base.h:96
virtual void spaceToProfile(std::vector< double > &io) const
Convert from the space's format, to the profile's data format.
Definition base.cpp:61
bool parse(std::istringstream &input, std::vector< double > &output) const override
Definition rgb.cpp:73
uint32_t _to_rgba(std::vector< double > const &values, double opacity) const
Definition rgb.cpp:60
std::shared_ptr< Colors::CMS::Profile > const getProfile() const override
Return the RGB color profile, this is static for all RGB sub-types.
Definition rgb.cpp:23
std::string toString(std::vector< double > const &values, bool opacity=true) const override
Print the RGB color to a CSS Hex code of 6 or 8 digits.
Definition rgb.cpp:42
uint32_t toRGBA(std::vector< double > const &values, double opacity=1.0) const override
Convert the color into an RGBA32 for use within Gdk rendering.
Definition rgb.cpp:50
constexpr uint32_t SP_RGBA32_F_COMPOSE(double r, double g, double b, double a)
Definition utils.h:64
Geom::Point end
std::string rgba_to_hex(uint32_t value, bool alpha)
Output the RGBA value as a #RRGGBB hex color, if alpha is true then the output will be #RRGGBBAA inst...
Definition utils.cpp:70
STL namespace.
Authors: see git history.
Glib::ustring name
Definition toolbars.cpp:55