KColorUtils

KColorUtils Namespace Reference

Functions

KGUIADDONS_EXPORT qreal chroma (const QColor &)
 
KGUIADDONS_EXPORT qreal contrastRatio (const QColor &, const QColor &)
 
KGUIADDONS_EXPORT QColor darken (const QColor &, qreal amount=0.5, qreal chromaGain=1.0)
 
KGUIADDONS_EXPORT void getHcy (const QColor &, qreal *hue, qreal *chroma, qreal *luma, qreal *alpha=nullptr)
 
KGUIADDONS_EXPORT QColor hcyColor (qreal hue, qreal chroma, qreal luma, qreal alpha=1.0)
 
KGUIADDONS_EXPORT qreal hue (const QColor &)
 
KGUIADDONS_EXPORT QColor lighten (const QColor &, qreal amount=0.5, qreal chromaInverseGain=1.0)
 
KGUIADDONS_EXPORT qreal luma (const QColor &)
 
KGUIADDONS_EXPORT QColor mix (const QColor &c1, const QColor &c2, qreal bias=0.5)
 
KGUIADDONS_EXPORT QColor overlayColors (const QColor &base, const QColor &paint, QPainter::CompositionMode comp=QPainter::CompositionMode_SourceOver)
 
KGUIADDONS_EXPORT QColor shade (const QColor &, qreal lumaAmount, qreal chromaAmount=0.0)
 
KGUIADDONS_EXPORT QColor tint (const QColor &base, const QColor &color, qreal amount=0.3)
 

Detailed Description

A set of methods used to work with colors.

Function Documentation

◆ chroma()

qreal KColorUtils::chroma ( const QColor & color)

Calculate the chroma of a color.

The range is from 0.0 (none) to 1.0 (full).

The result is computed in linear (not sRGB) color space.

See also
https://en.wikipedia.org/wiki/Colorfulness
Since
5.68

Definition at line 30 of file kcolorutils.cpp.

◆ contrastRatio()

qreal KColorUtils::contrastRatio ( const QColor & c1,
const QColor & c2 )

Calculate the contrast ratio between two colors, according to the W3C/WCAG2.0 algorithm, (Lmax + 0.05)/(Lmin + 0.05), where Lmax and Lmin are the luma values of the lighter color and the darker color, respectively.

A contrast ration of 5:1 (result == 5.0) is the minimum for "normal" text to be considered readable (large text can go as low as 3:1). The ratio ranges from 1:1 (result == 1.0) to 21:1 (result == 21.0).

See also
KColorUtils::luma

Definition at line 68 of file kcolorutils.cpp.

◆ darken()

QColor KColorUtils::darken ( const QColor & color,
qreal amount = 0.5,
qreal chromaGain = 1.0 )

Adjust the luma of a color by changing its distance from black.

  • amount == 1.0 gives black
  • amount == 0.5 results in a color whose luma is halfway between 0.0 and that of the original color
  • amount == 0.0 gives the original color
  • amount == -1.0 gives a color that is 'twice as far from black' as the original color, that is luma(result) == 2*luma(color)
Parameters
amountfactor by which to adjust the luma component of the color
chromaGain(optional) factor by which to adjust the chroma component of the color; 1.0 means no change, 0.0 minimizes chroma
See also
KColorUtils::shade

Definition at line 81 of file kcolorutils.cpp.

◆ getHcy()

void KColorUtils::getHcy ( const QColor & color,
qreal * hue,
qreal * chroma,
qreal * luma,
qreal * alpha = nullptr )

Calculate hue, chroma and luma of a color in one call.

The range of hue is from 0.0 (red) to almost 1.0 (slightly blue-ish red). The range of chroma is from 0.0 (none) to 1.0 (full). The range of luma is from 0.0 (black) to 1.0 (white).

The hue, chroma and luma values are computed in linear (not sRGB) color space.

Since
5.0

Definition at line 40 of file kcolorutils.cpp.

◆ hcyColor()

QColor KColorUtils::hcyColor ( qreal hue,
qreal chroma,
qreal luma,
qreal alpha = 1.0 )

Return a QColor based on the given hue, chroma, luma and alpha values.

The range of hue is cyclical. For example, 0.0 and 1.0 are both red while -0.166667 and 0.833333 are both magenta. The range of chroma is from 0.0 (none) to 1.0 (full). Out of range values will be clamped. The range of luma is from 0.0 (black) to 1.0 (white). Out of range values will be clamped.

The hue, chroma and luma values are computed in linear (not sRGB) color space.

Since
5.68

Definition at line 54 of file kcolorutils.cpp.

◆ hue()

qreal KColorUtils::hue ( const QColor & color)

Calculate the hue of a color.

The range is from 0.0 (red) to almost 1.0 (slightly blue-ish red).

The result is computed in linear (not sRGB) color space and may differ slightly from QColor::hue().

See also
https://en.wikipedia.org/wiki/Hue
Since
5.68

Definition at line 25 of file kcolorutils.cpp.

◆ lighten()

QColor KColorUtils::lighten ( const QColor & color,
qreal amount = 0.5,
qreal chromaInverseGain = 1.0 )

Adjust the luma of a color by changing its distance from white.

  • amount == 1.0 gives white
  • amount == 0.5 results in a color whose luma is halfway between 1.0 and that of the original color
  • amount == 0.0 gives the original color
  • amount == -1.0 gives a color that is 'twice as far from white' as the original color, that is luma(result) == 1.0 - 2*(1.0 - luma(color))
Parameters
amountfactor by which to adjust the luma component of the color
chromaInverseGain(optional) factor by which to adjust the chroma component of the color; 1.0 means no change, 0.0 maximizes chroma
See also
KColorUtils::shade

Definition at line 73 of file kcolorutils.cpp.

◆ luma()

qreal KColorUtils::luma ( const QColor & color)

Calculate the luma of a color.

Luma is weighted sum of gamma-adjusted R'G'B' components of a color. The result is similar to qGray. The range is from 0.0 (black) to 1.0 (white).

The result is computed in linear (not sRGB) color space.

KColorUtils::darken(), KColorUtils::lighten() and KColorUtils::shade() operate on the luma of a color.

See also
http://en.wikipedia.org/wiki/Luma_(video)

Definition at line 35 of file kcolorutils.cpp.

◆ mix()

QColor KColorUtils::mix ( const QColor & c1,
const QColor & c2,
qreal bias = 0.5 )

Blend two colors into a new color by linear combination.

QColor lighter = KColorUtils::mix(myColor, Qt::white)
KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
Blend two colors into a new color by linear combination.
Parameters
c1first color.
c2second color.
biasweight to be used for the mix. bias <= 0 gives c1, bias >= 1 gives c2. bias == 0.5 gives a 50% blend of c1 and c2.

Definition at line 144 of file kcolorutils.cpp.

◆ overlayColors()

QColor KColorUtils::overlayColors ( const QColor & base,
const QColor & paint,
QPainter::CompositionMode comp = QPainter::CompositionMode_SourceOver )

Blend two colors into a new color by painting the second color over the first using the specified composition mode.

white.setAlphaF(0.5);
QColor lighter = KColorUtils::overlayColors(myColor, white);
KGUIADDONS_EXPORT QColor overlayColors(const QColor &base, const QColor &paint, QPainter::CompositionMode comp=QPainter::CompositionMode_SourceOver)
Blend two colors into a new color by painting the second color over the first using the specified com...
Parameters
basethe base color (alpha channel is ignored).
paintthe color to be overlaid onto the base color.
compthe CompositionMode used to do the blending.

Definition at line 168 of file kcolorutils.cpp.

◆ shade()

QColor KColorUtils::shade ( const QColor & color,
qreal lumaAmount,
qreal chromaAmount = 0.0 )

Adjust the luma and chroma components of a color.

The amount is added to the corresponding component.

Parameters
lumaAmountamount by which to adjust the luma component of the color; 0.0 results in no change, -1.0 turns anything black, 1.0 turns anything white
chromaAmount(optional) amount by which to adjust the chroma component of the color; 0.0 results in no change, -1.0 minimizes chroma, 1.0 maximizes chroma
See also
KColorUtils::luma

Definition at line 89 of file kcolorutils.cpp.

◆ tint()

QColor KColorUtils::tint ( const QColor & base,
const QColor & color,
qreal amount = 0.3 )

Create a new color by tinting one color with another.

This function is meant for creating additional colors withings the same class (background, foreground) from colors in a different class. Therefore when amount is low, the luma of base is mostly preserved, while the hue and chroma of color is mostly inherited.

Parameters
basecolor to be tinted
colorcolor with which to tint
amounthow strongly to tint the base; 0.0 gives base, 1.0 gives color

Definition at line 113 of file kcolorutils.cpp.

This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:52:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.