File indexing completed on 2024-04-21 14:56:36

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
0004     SPDX-FileCopyrightText: 2007 Zack Rusin <zack@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KCOLORUTILS_H
0010 #define KCOLORUTILS_H
0011 
0012 #include <kguiaddons_export.h>
0013 
0014 #include <QPainter>
0015 
0016 class QColor;
0017 
0018 /**
0019  * A set of methods used to work with colors.
0020  */
0021 namespace KColorUtils
0022 {
0023 /**
0024  * Calculate the hue of a color. The range is from 0.0 (red) to almost 1.0 (slightly blue-ish red).
0025  *
0026  * The result is computed in linear (not sRGB) color space and may differ slightly from QColor::hue().
0027  *
0028  * @see https://en.wikipedia.org/wiki/Hue
0029  * @since 5.68
0030  */
0031 KGUIADDONS_EXPORT qreal hue(const QColor &);
0032 
0033 /**
0034  * Calculate the chroma of a color. The range is from 0.0 (none) to 1.0 (full).
0035  *
0036  * The result is computed in linear (not sRGB) color space.
0037  *
0038  * @see https://en.wikipedia.org/wiki/Colorfulness
0039  * @since 5.68
0040  */
0041 KGUIADDONS_EXPORT qreal chroma(const QColor &);
0042 
0043 /**
0044  * Calculate the luma of a color. Luma is weighted sum of gamma-adjusted
0045  * R'G'B' components of a color. The result is similar to qGray. The range
0046  * is from 0.0 (black) to 1.0 (white).
0047  *
0048  * The result is computed in linear (not sRGB) color space.
0049  *
0050  * KColorUtils::darken(), KColorUtils::lighten() and KColorUtils::shade()
0051  * operate on the luma of a color.
0052  *
0053  * @see http://en.wikipedia.org/wiki/Luma_(video)
0054  */
0055 KGUIADDONS_EXPORT qreal luma(const QColor &);
0056 
0057 /**
0058  * Calculate hue, chroma and luma of a color in one call.
0059  *
0060  * The range of hue is from 0.0 (red) to almost 1.0 (slightly blue-ish red).
0061  * The range of chroma is from 0.0 (none) to 1.0 (full).
0062  * The range of luma is from 0.0 (black) to 1.0 (white).
0063  *
0064  * The hue, chroma and luma values are computed in linear (not sRGB) color space.
0065  *
0066  * @since 5.0
0067  */
0068 KGUIADDONS_EXPORT void getHcy(const QColor &, qreal *hue, qreal *chroma, qreal *luma, qreal *alpha = nullptr);
0069 
0070 /**
0071  * Return a QColor based on the given hue, chroma, luma and alpha values.
0072  *
0073  * 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.
0074  * The range of chroma is from 0.0 (none) to 1.0 (full). Out of range values will be clamped.
0075  * The range of luma is from 0.0 (black) to 1.0 (white). Out of range values will be clamped.
0076  *
0077  * The hue, chroma and luma values are computed in linear (not sRGB) color space.
0078  *
0079  * @since 5.68
0080  */
0081 KGUIADDONS_EXPORT QColor hcyColor(qreal hue, qreal chroma, qreal luma, qreal alpha = 1.0);
0082 
0083 /**
0084  * Calculate the contrast ratio between two colors, according to the
0085  * W3C/WCAG2.0 algorithm, (Lmax + 0.05)/(Lmin + 0.05), where Lmax and Lmin
0086  * are the luma values of the lighter color and the darker color,
0087  * respectively.
0088  *
0089  * A contrast ration of 5:1 (result == 5.0) is the minimum for "normal"
0090  * text to be considered readable (large text can go as low as 3:1). The
0091  * ratio ranges from 1:1 (result == 1.0) to 21:1 (result == 21.0).
0092  *
0093  * @see KColorUtils::luma
0094  */
0095 KGUIADDONS_EXPORT qreal contrastRatio(const QColor &, const QColor &);
0096 
0097 /**
0098  * Adjust the luma of a color by changing its distance from white.
0099  *
0100  * @li amount == 1.0 gives white
0101  * @li amount == 0.5 results in a color whose luma is halfway between 1.0
0102  * and that of the original color
0103  * @li amount == 0.0 gives the original color
0104  * @li amount == -1.0 gives a color that is 'twice as far from white' as
0105  * the original color, that is luma(result) == 1.0 - 2*(1.0 - luma(color))
0106  *
0107  * @param amount factor by which to adjust the luma component of the color
0108  * @param chromaInverseGain (optional) factor by which to adjust the chroma
0109  * component of the color; 1.0 means no change, 0.0 maximizes chroma
0110  * @see KColorUtils::shade
0111  */
0112 KGUIADDONS_EXPORT QColor lighten(const QColor &, qreal amount = 0.5, qreal chromaInverseGain = 1.0);
0113 
0114 /**
0115  * Adjust the luma of a color by changing its distance from black.
0116  *
0117  * @li amount == 1.0 gives black
0118  * @li amount == 0.5 results in a color whose luma is halfway between 0.0
0119  * and that of the original color
0120  * @li amount == 0.0 gives the original color
0121  * @li amount == -1.0 gives a color that is 'twice as far from black' as
0122  * the original color, that is luma(result) == 2*luma(color)
0123  *
0124  * @param amount factor by which to adjust the luma component of the color
0125  * @param chromaGain (optional) factor by which to adjust the chroma
0126  * component of the color; 1.0 means no change, 0.0 minimizes chroma
0127  * @see KColorUtils::shade
0128  */
0129 KGUIADDONS_EXPORT QColor darken(const QColor &, qreal amount = 0.5, qreal chromaGain = 1.0);
0130 
0131 /**
0132  * Adjust the luma and chroma components of a color. The amount is added
0133  * to the corresponding component.
0134  *
0135  * @param lumaAmount amount by which to adjust the luma component of the
0136  * color; 0.0 results in no change, -1.0 turns anything black, 1.0 turns
0137  * anything white
0138  * @param chromaAmount (optional) amount by which to adjust the chroma
0139  * component of the color; 0.0 results in no change, -1.0 minimizes chroma,
0140  * 1.0 maximizes chroma
0141  * @see KColorUtils::luma
0142  */
0143 KGUIADDONS_EXPORT QColor shade(const QColor &, qreal lumaAmount, qreal chromaAmount = 0.0);
0144 
0145 /**
0146  * Create a new color by tinting one color with another. This function is
0147  * meant for creating additional colors withings the same class (background,
0148  * foreground) from colors in a different class. Therefore when @p amount
0149  * is low, the luma of @p base is mostly preserved, while the hue and
0150  * chroma of @p color is mostly inherited.
0151  *
0152  * @param base color to be tinted
0153  * @param color color with which to tint
0154  * @param amount how strongly to tint the base; 0.0 gives @p base,
0155  * 1.0 gives @p color
0156  */
0157 KGUIADDONS_EXPORT QColor tint(const QColor &base, const QColor &color, qreal amount = 0.3);
0158 
0159 /**
0160  * Blend two colors into a new color by linear combination.
0161  * @code
0162     QColor lighter = KColorUtils::mix(myColor, Qt::white)
0163  * @endcode
0164  * @param c1 first color.
0165  * @param c2 second color.
0166  * @param bias weight to be used for the mix. @p bias <= 0 gives @p c1,
0167  * @p bias >= 1 gives @p c2. @p bias == 0.5 gives a 50% blend of @p c1
0168  * and @p c2.
0169  */
0170 KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias = 0.5);
0171 
0172 /**
0173  * Blend two colors into a new color by painting the second color over the
0174  * first using the specified composition mode.
0175  * @code
0176     QColor white(Qt::white);
0177     white.setAlphaF(0.5);
0178     QColor lighter = KColorUtils::overlayColors(myColor, white);
0179    @endcode
0180  * @param base the base color (alpha channel is ignored).
0181  * @param paint the color to be overlaid onto the base color.
0182  * @param comp the CompositionMode used to do the blending.
0183  */
0184 KGUIADDONS_EXPORT QColor overlayColors(const QColor &base, const QColor &paint, QPainter::CompositionMode comp = QPainter::CompositionMode_SourceOver);
0185 }
0186 
0187 #endif // KCOLORUTILS_H