Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
Inkscape::Colors::Color Class Referencefinal

#include <color.h>

Public Member Functions

 Color (std::shared_ptr< Space::AnySpace > space, std::vector< double > colors)
 Construct a color in the given color space.
 
 Color (Space::Type space_type, std::vector< double > values)
 Create a color, given the type of the space and the values to store.
 
 Color (uint32_t color, bool alpha=true)
 Compatability layer for making blind RGB colors.
 
bool operator== (Color const &other) const
 Return true if the two colors are the same.
 
double operator[] (unsigned int index) const
 
std::shared_ptr< Space::AnySpace > const & getSpace () const
 
const std::vector< double > & getValues () const
 
void setValues (std::vector< double > values)
 Set the channels directly without checking if the space is correct.
 
size_t size () const
 
double get (unsigned int index) const
 Get a single channel from this color.
 
bool set (unsigned int index, double value)
 Set a specific channel in the color.
 
bool set (Color const &other, bool keep_space=true)
 Set this color to the values from another color.
 
bool set (std::string const &parsable, bool keep_space=true)
 Set this color by parsing the given string.
 
bool set (uint32_t rgba, bool opacity=true)
 Set this color from an RGBA unsigned int.
 
bool hasOpacity () const
 Returns true if there is an opacity channel in this color.
 
void enableOpacity (bool enabled)
 Enables or disables the opacity channel.
 
unsigned int getOpacityChannel () const
 Get the opacity channel index.
 
double getOpacity () const
 Get the opacity in this color, if it's stored.
 
double stealOpacity ()
 Get the opacity, and remove it from this color.
 
bool setOpacity (double opacity)
 Set the opacity of this color object.
 
bool addOpacity (double opacity=1.0)
 
unsigned int getPin (unsigned int channel) const
 Return the pin number (pow2) of the channel index to pin that channel in a mutation.
 
double difference (Color const &other) const
 Get the mean square difference between this color and another.
 
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.
 
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.
 
bool convert (Color const &other)
 Convert to the same format as the other color.
 
bool convert (std::shared_ptr< Space::AnySpace > space)
 Convert this color into a different color space.
 
bool convert (Space::Type type)
 Convert this color into the first matched color space of the given type.
 
std::optional< Colorconverted (Color const &other) const
 Return a copy of this color converted to the same format as the other color.
 
std::optional< Colorconverted (std::shared_ptr< Space::AnySpace > to_space) const
 Convert a copy of this color into a different color space.
 
std::optional< Colorconverted (Space::Type type) const
 Convert a copy of this color into the first matching color space type.
 
std::string toString (bool opacity=true) const
 Format the color as a css string and return it.
 
uint32_t toRGBA (double opacity=1.0) const
 Return an sRGB conversion of the color in RGBA int32 format.
 
uint32_t toARGB (double opacity=1.0) const
 Return the RGBA int32 as an ARGB format number.
 
uint32_t toABGR (double opacity=1.0) const
 Return the RGBA int32 as an ABGR format color.
 
std::string getName () const
 
void setName (std::string name)
 
bool isOutOfGamut (std::shared_ptr< Space::AnySpace > other) const
 Return true if this color would be out of gamut when converted to another space.
 
bool isOverInked () const
 Return true if this color would be considered over-inked.
 
void normalize ()
 Make sure the values for this color are within acceptable ranges.
 
Color normalized () const
 Return a normalized copy of this color so the values are within acceptable ranges.
 
void compose (Color const &other)
 Put the other color on top of this color, mixing the two according to the alpha.
 
Color composed (Color const &other) const
 Return the composition of this color, plus the other color on top.
 
void average (Color const &other, double pos=0.5, unsigned int pin=0)
 
Color averaged (Color const &other, double pos=0.5) const
 Return the average between this and another color.
 
void invert (unsigned int pin)
 Invert the color for each channel.
 
void invert ()
 
void jitter (double force, unsigned int pin=0)
 Jitter the color for each channel.
 

Static Public Member Functions

static std::optional< Colorparse (char const *value)
 Create an optional color if value is valid.
 
static std::optional< Colorparse (std::string const &value)
 Create an optional color, if possible, from the given string.
 
static std::optional< ColorifValid (Space::Type space_type, std::vector< double > values)
 Construct a color from the space type and values, if the values are valid.
 

Static Public Attributes

static constexpr double EPSILON = 1e-4
 

Private Member Functions

template<typename Func >
void _color_mutate_inplace (Color const &other, unsigned int pin, Func avgFunc)
 
bool _isnear (std::vector< double > const &other, double epsilon=0.001) const
 Returns true if the values are near to the other values.
 

Private Attributes

std::string _name
 
std::vector< double > _values
 
std::shared_ptr< Space::AnySpace_space
 

Detailed Description

Definition at line 25 of file color.h.

Constructor & Destructor Documentation

◆ Color() [1/3]

Inkscape::Colors::Color::Color ( std::shared_ptr< Space::AnySpace space,
std::vector< double >  colors 
)

Construct a color in the given color space.

  • space - The color space these channel values exist in.
  • colors - Each channel in the color space must have a value between 0.0 and 1.0 an extra value may be appended to indicate the opacity to support CSS formatted opacity parsing but is not expected to be writen by Inkscape when being generated.

Definition at line 60 of file color.cpp.

References _space, and _values.

◆ Color() [2/3]

Inkscape::Colors::Color::Color ( Space::Type  space_type,
std::vector< double >  values 
)

Create a color, given the type of the space and the values to store.

  • space_type - The type of the color space these values are in.
  • values - A vector of numbers usually between 0.0 and 1.0 per channel which will be moved to the new Color object.

Definition at line 40 of file color.cpp.

◆ Color() [3/3]

Inkscape::Colors::Color::Color ( uint32_t  color,
bool  alpha = true 
)
explicit

Compatability layer for making blind RGB colors.

Definition at line 47 of file color.cpp.

Member Function Documentation

◆ _color_mutate_inplace()

template<typename Func >
void Inkscape::Colors::Color::_color_mutate_inplace ( Color const &  other,
unsigned int  pin,
Func  avgFunc 
)
private

Definition at line 630 of file color.cpp.

References _color_mutate_inplace(), _space, _values, converted(), and hasOpacity().

Referenced by _color_mutate_inplace(), average(), and compose().

◆ _isnear()

bool Inkscape::Colors::Color::_isnear ( std::vector< double > const &  other,
double  epsilon = 0.001 
) const
private

Returns true if the values are near to the other values.

Definition at line 292 of file color.cpp.

References _values.

Referenced by operator==(), and set().

◆ addOpacity()

◆ average()

void Inkscape::Colors::Color::average ( Color const &  other,
double  pos = 0.5,
unsigned int  pin = 0 
)

Definition at line 544 of file color.cpp.

References _color_mutate_inplace().

Referenced by Inkscape::UI::Tools::tweak_color().

◆ averaged()

Color Inkscape::Colors::Color::averaged ( Color const &  other,
double  pos = 0.5 
) const

Return the average between this and another color.

  • other - The other color to average with
  • pos - The weighting to give each color

Definition at line 556 of file color.cpp.

Referenced by Inkscape::UI::Tools::GradientTool::simplify(), sp_gradient_add_stop(), and sp_vector_add_stop().

◆ compose()

void Inkscape::Colors::Color::compose ( Color const &  other)

Put the other color on top of this color, mixing the two according to the alpha.

  • other - The other color to compose with this one.

Definition at line 517 of file color.cpp.

References _color_mutate_inplace(), getOpacity(), getOpacityChannel(), getPin(), and setOpacity().

◆ composed()

Color Inkscape::Colors::Color::composed ( Color const &  other) const

Return the composition of this color, plus the other color on top.

  • other - The other color to compose with this one.

Definition at line 530 of file color.cpp.

Referenced by Inkscape::UI::Tools::DropperTool::root_handler().

◆ convert() [1/3]

bool Inkscape::Colors::Color::convert ( Color const &  other)

Convert to the same format as the other color.

  • other - Another color to copy the space and opacity from.

Definition at line 145 of file color.cpp.

References _space, convert(), enableOpacity(), and hasOpacity().

Referenced by convert(), convert(), set(), sp_gradient_to_hhssll(), and Inkscape::Extension::Internal::LaTeXTextRenderer::sp_text_render().

◆ convert() [2/3]

bool Inkscape::Colors::Color::convert ( Space::Type  type)

Convert this color into the first matched color space of the given type.

Definition at line 178 of file color.cpp.

References convert(), and Inkscape::Colors::Manager::get().

◆ convert() [3/3]

bool Inkscape::Colors::Color::convert ( std::shared_ptr< Space::AnySpace to_space)

Convert this color into a different color space.

  • to_space - The target space to convert the color values to

Definition at line 159 of file color.cpp.

References _name, _space, and _values.

◆ converted() [1/3]

◆ converted() [2/3]

std::optional< Color > Inkscape::Colors::Color::converted ( Space::Type  type) const

Convert a copy of this color into the first matching color space type.

if the space is not available in this color manager, an empty color is returned.

Definition at line 215 of file color.cpp.

◆ converted() [3/3]

std::optional< Color > Inkscape::Colors::Color::converted ( std::shared_ptr< Space::AnySpace to_space) const

Convert a copy of this color into a different color space.

Definition at line 201 of file color.cpp.

◆ difference()

double Inkscape::Colors::Color::difference ( Color const &  other) const

Get the mean square difference between this color and another.

Definition at line 566 of file color.cpp.

References _values, and converted().

◆ enableOpacity()

void Inkscape::Colors::Color::enableOpacity ( bool  enabled)

Enables or disables the opacity channel.

Definition at line 385 of file color.cpp.

References _values, and hasOpacity().

Referenced by convert(), set(), sp_gradstop_to_pixbuf_ref(), and stealOpacity().

◆ get()

double Inkscape::Colors::Color::get ( unsigned int  index) const

Get a single channel from this color.

Definition at line 87 of file color.cpp.

References _values, getOpacityChannel(), and index.

Referenced by operator[]().

◆ getName()

std::string Inkscape::Colors::Color::getName ( ) const
inline

Definition at line 78 of file color.h.

References _name.

Referenced by Inkscape::UI::Dialog::ColorItem::ColorItem().

◆ getOpacity()

◆ getOpacityChannel()

unsigned int Inkscape::Colors::Color::getOpacityChannel ( ) const

Get the opacity channel index.

Definition at line 427 of file color.cpp.

References _space.

Referenced by compose(), get(), hasOpacity(), invert(), and set().

◆ getPin()

unsigned int Inkscape::Colors::Color::getPin ( unsigned int  channel) const

Return the pin number (pow2) of the channel index to pin that channel in a mutation.

Definition at line 436 of file color.cpp.

Referenced by compose(), and invert().

◆ getSpace()

◆ getValues()

const std::vector< double > & Inkscape::Colors::Color::getValues ( ) const
inline

◆ hasOpacity()

bool Inkscape::Colors::Color::hasOpacity ( ) const

Returns true if there is an opacity channel in this color.

Definition at line 398 of file color.cpp.

References _values, and getOpacityChannel().

Referenced by _color_mutate_inplace(), SPNamedView::change_color(), convert(), enableOpacity(), getOpacity(), normalize(), set(), and setOpacity().

◆ ifValid()

std::optional< Color > Inkscape::Colors::Color::ifValid ( Space::Type  space_type,
std::vector< double >  values 
)
static

Construct a color from the space type and values, if the values are valid.

Definition at line 331 of file color.cpp.

References Inkscape::Colors::Manager::get().

Referenced by parse().

◆ invert() [1/2]

void Inkscape::Colors::Color::invert ( )
inline

Definition at line 94 of file color.h.

References getOpacityChannel(), getPin(), and invert().

Referenced by invert().

◆ invert() [2/2]

void Inkscape::Colors::Color::invert ( unsigned int  pin)

Invert the color for each channel.

  • pin - Bit field, which channels should not change if not specified the opacity pin is used as this is the most reasonable default.

Definition at line 483 of file color.cpp.

References _values.

◆ isClose()

bool Inkscape::Colors::Color::isClose ( Color const &  other,
double  epsilon = EPSILON 
) const

Find out if a color is a close match to another color of the same type.

Returns
true if the colors are the same structure (space, opacity) and have values which are no more than epison apart.

Definition at line 583 of file color.cpp.

References _space, and _values.

Referenced by SPGradient::isEquivalent(), and isSimilar().

◆ isOutOfGamut()

bool Inkscape::Colors::Color::isOutOfGamut ( std::shared_ptr< Space::AnySpace other) const

Return true if this color would be out of gamut when converted to another space.

  • other - The other color space to compare against.

Definition at line 615 of file color.cpp.

References _space, and _values.

◆ isOverInked()

bool Inkscape::Colors::Color::isOverInked ( ) const

Return true if this color would be considered over-inked.

Definition at line 623 of file color.cpp.

References _space, and _values.

◆ isSimilar()

bool Inkscape::Colors::Color::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.

If one has opacity and the other does not it always returns false.

Returns
true if the colors are similar when converted to the same space.

Definition at line 599 of file color.cpp.

References _space, converted(), and isClose().

Referenced by sp_get_same_fill_or_stroke_color().

◆ jitter()

void Inkscape::Colors::Color::jitter ( double  force,
unsigned int  pin = 0 
)

Jitter the color for each channel.

@

  • force - The amount of jitter to add to each channel.
  • pin - Bit field, which channels should not change (see invert).

Definition at line 499 of file color.cpp.

References _values, and normalize().

◆ normalize()

void Inkscape::Colors::Color::normalize ( )

Make sure the values for this color are within acceptable ranges.

Definition at line 460 of file color.cpp.

References _space, _values, and hasOpacity().

Referenced by Inkscape::UI::Widget::OKWheel::_setColor(), jitter(), and Inkscape::UI::Widget::ColorWheelHSL::on_key_pressed().

◆ normalized()

Color Inkscape::Colors::Color::normalized ( ) const

Return a normalized copy of this color so the values are within acceptable ranges.

Definition at line 470 of file color.cpp.

◆ operator==()

bool Inkscape::Colors::Color::operator== ( Color const &  other) const

Return true if the two colors are the same.

The color space AND the values must be the same. But the name doesn't not have to be the same in both colors.

Definition at line 73 of file color.cpp.

References _isnear(), _space, and _values.

◆ operator[]()

double Inkscape::Colors::Color::operator[] ( unsigned int  index) const
inline

Definition at line 37 of file color.h.

References get(), and index.

◆ parse() [1/2]

std::optional< Color > Inkscape::Colors::Color::parse ( char const *  value)
static

Create an optional color if value is valid.

Definition at line 304 of file color.cpp.

References parse().

Referenced by parse(), and parse().

◆ parse() [2/2]

std::optional< Color > Inkscape::Colors::Color::parse ( std::string const &  value)
static

Create an optional color, if possible, from the given string.

Definition at line 315 of file color.cpp.

References Inkscape::Colors::Parsers::get(), ifValid(), and parse().

◆ set() [1/4]

bool Inkscape::Colors::Color::set ( Color const &  other,
bool  keep_space = true 
)

Set this color to the values from another color.

  • other - The other color which is a source for the values. if the other color is from an unknown color space which has never been seen before, it will cause an error.
  • keep_space - If true, this color's color-space will stay the same and the new values will be converted. This includes discarding opacity if this color didn't have opacity.
Returns
true if the new value if different from the old value

Definition at line 249 of file color.cpp.

References _isnear(), _name, _space, _values, convert(), enableOpacity(), hasOpacity(), and set.

◆ set() [2/4]

bool Inkscape::Colors::Color::set ( std::string const &  parsable,
bool  keep_space = true 
)

Set this color by parsing the given string.

If there's a parser error it will not change the existing color.

  • parsable - A string with a typical css color value.
  • keep_space - If true, the existing space will stay the same (see previous Color::set)
Returns
true if the new value if different from the old value

Definition at line 281 of file color.cpp.

References set.

◆ set() [3/4]

bool Inkscape::Colors::Color::set ( uint32_t  rgba,
bool  opacity = true 
)

Set this color from an RGBA unsigned int.

  • rgba - The RGBA color encoded as a single unsigned integer, 8bpc
  • opacity - True if the opacity (Alpha) should be stored too.
Returns
true if the new value if different from the old value

Definition at line 369 of file color.cpp.

References _name, _space, _values, Inkscape::Colors::Manager::get(), Inkscape::Colors::Space::RGB, Inkscape::Colors::rgba_to_values(), and toRGBA().

◆ set() [4/4]

◆ setName()

void Inkscape::Colors::Color::setName ( std::string  name)
inline

Definition at line 79 of file color.h.

References _name, and name.

◆ setOpacity()

bool Inkscape::Colors::Color::setOpacity ( double  opacity)

Set the opacity of this color object.

Definition at line 444 of file color.cpp.

References _values, and hasOpacity().

Referenced by addOpacity(), compose(), Inkscape::load_svg_cursor(), SPGrid::set(), Inkscape::UI::Widget::ColorPlate::set_base_color(), and Inkscape::PageManager::subset().

◆ setValues()

void Inkscape::Colors::Color::setValues ( std::vector< double >  values)

Set the channels directly without checking if the space is correct.

  • values - A vector of doubles, one value between 0.0 and 1.0 for each channel. An extra channel can be included for opacity.

Definition at line 230 of file color.cpp.

References _name, _space, and _values.

◆ size()

size_t Inkscape::Colors::Color::size ( ) const
inline

Definition at line 42 of file color.h.

References _values.

Referenced by Inkscape::Colors::fromMIMEData().

◆ stealOpacity()

double Inkscape::Colors::Color::stealOpacity ( )

Get the opacity, and remove it from this color.

This is useful when setting a color into an svg css property that has it's own opacity property but you aren't ready to create a string (see toString(false) for that use)

Definition at line 417 of file color.cpp.

References enableOpacity(), and getOpacity().

Referenced by Inkscape::UI::Tools::CalligraphicTool::brush(), Inkscape::UI::Widget::checkerboard_darken(), and Inkscape::UI::Widget::draw_circle().

◆ toABGR()

uint32_t Inkscape::Colors::Color::toABGR ( double  opacity = 1.0) const

Return the RGBA int32 as an ABGR format color.

Definition at line 134 of file color.cpp.

References toRGBA().

◆ toARGB()

uint32_t Inkscape::Colors::Color::toARGB ( double  opacity = 1.0) const

◆ toRGBA()

◆ toString()

std::string Inkscape::Colors::Color::toString ( bool  opacity = true) const

Format the color as a css string and return it.

  • opacity - If set to false the opacity will be ignored, even if present.

Note: Choose the color space for your color carefully before printing. If you are outputting to a CSS version that only supports RGB hex codes, then convert the color to that color space before printing.

Definition at line 106 of file color.cpp.

References _space, and _values.

Referenced by SPNamedView::change_color(), Inkscape::LivePathEffect::LPEMeasureSegments::createLine(), Inkscape::LivePathEffect::LPEMeasureSegments::createTextLabel(), Inkscape::Extension::Internal::PdfBuilder::Document::get_color(), Inkscape::load_svg_cursor(), GrDrag::makeStopSafeColor(), Inkscape::UI::Widget::RegisteredColorPicker::on_changed(), Inkscape::UI::Dialog::set_export_bg_color(), Inkscape::Preferences::setColor(), SPStop::setColorRepr(), SPGrid::setMajorColor(), SPGrid::setMinorColor(), and Inkscape::Extension::ParamColor::value_to_string().

Member Data Documentation

◆ _name

std::string Inkscape::Colors::Color::_name
private

Definition at line 98 of file color.h.

Referenced by convert(), getName(), set(), set(), setName(), and setValues().

◆ _space

std::shared_ptr<Space::AnySpace> Inkscape::Colors::Color::_space
private

◆ _values

◆ EPSILON

constexpr double Inkscape::Colors::Color::EPSILON = 1e-4
staticconstexpr

Definition at line 60 of file color.h.


The documentation for this class was generated from the following files: