File indexing completed on 2024-04-14 03:52:24

0001 /* vi: ts=8 sts=4 sw=4
0002 
0003     This file is part of the KDE project, module kdecore.
0004     SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
0005     SPDX-FileCopyrightText: 2007 Daniel M. Duley <daniel.duley@verizon.net>
0006 
0007     with minor additions and based on ideas from
0008     SPDX-FileContributor: Torsten Rahn <torsten@kde.org>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-only
0011 */
0012 
0013 #ifndef KICONEFFECT_H
0014 #define KICONEFFECT_H
0015 
0016 #include <kiconthemes_export.h>
0017 
0018 #include <QColor>
0019 #include <QImage>
0020 #include <QPixmap>
0021 
0022 #include <memory>
0023 
0024 class KIconEffectPrivate;
0025 
0026 /**
0027  * @class KIconEffect kiconeffect.h KIconEffect
0028  *
0029  * Applies effects to icons.
0030  *
0031  * This class applies effects to icons depending on their state and
0032  * group. For example, it can be used to make all disabled icons
0033  * in a toolbar gray.
0034  *
0035  * \image html kiconeffect-apply.png "Various Effects applied to an image"
0036  *
0037  * @see QIcon::fromTheme
0038  */
0039 class KICONTHEMES_EXPORT KIconEffect
0040 {
0041 public:
0042     /**
0043      * Create a new KIconEffect.
0044      * You will most likely never have to use this to create a new KIconEffect
0045      * yourself, as you can use the KIconEffect provided by the global KIconLoader
0046      * (which itself is accessible by KIconLoader::global()) through its
0047      * iconEffect() function.
0048      */
0049     KIconEffect();
0050     ~KIconEffect();
0051 
0052     KIconEffect(const KIconEffect &) = delete;
0053     KIconEffect &operator=(const KIconEffect &) = delete;
0054 
0055     /**
0056      * This is the enumeration of all possible icon effects.
0057      * Note that 'LastEffect' is no valid icon effect but only
0058      * used internally to check for invalid icon effects.
0059      *
0060      * @li NoEffect: Do not apply any icon effect
0061      * @li ToGray: Tints the icon gray
0062      * @li Colorize: Tints the icon with a specific color
0063      * @li ToGamma: Change the gamma value of the icon
0064      * @li DeSaturate: Reduce the saturation of the icon
0065      * @li ToMonochrome: Produces a monochrome icon
0066      */
0067     enum Effects {
0068         NoEffect,
0069         ToGray,
0070         Colorize,
0071         ToGamma,
0072         DeSaturate,
0073         ToMonochrome,
0074         LastEffect,
0075     };
0076 
0077     /**
0078      * Rereads configuration.
0079      */
0080     void init();
0081 
0082     /**
0083      * Tests whether an effect has been configured for the given icon group.
0084      * @param group the group to check, see KIconLoader::Group
0085      * @param state the state to check, see KIconLoader::States
0086      * @returns true if an effect is configured for the given @p group
0087      * in @p state, otherwise false.
0088      * @see KIconLoader::Group
0089      * KIconLoader::States
0090      */
0091     bool hasEffect(int group, int state) const;
0092 
0093     /**
0094      * Returns a fingerprint for the effect by encoding
0095      * the given @p group and @p state into a QString. This
0096      * is useful for caching.
0097      * @param group the group, see KIconLoader::Group
0098      * @param state the state, see KIconLoader::States
0099      * @return the fingerprint of the given @p group+@p state
0100      */
0101     QString fingerprint(int group, int state) const;
0102 
0103     /**
0104      * Applies an effect to an image. The effect to apply depends on the
0105      * @p group and @p state parameters, and is configured by the user.
0106      * @param src The image.
0107      * @param group The group for the icon, see KIconLoader::Group
0108      * @param state The icon's state, see KIconLoader::States
0109      * @return An image with the effect applied.
0110      */
0111     QImage apply(const QImage &src, int group, int state) const;
0112 
0113     /**
0114      * Applies an effect to an image.
0115      * @param src The image.
0116      * @param effect The effect to apply, one of KIconEffect::Effects.
0117      * @param value Strength of the effect. 0 <= @p value <= 1.
0118      * @param rgb Color parameter for effects that need one.
0119      * @param trans Add Transparency if trans = true.
0120      * @return An image with the effect applied.
0121      */
0122     QImage apply(const QImage &src, int effect, float value, const QColor &rgb, bool trans) const;
0123     QImage apply(const QImage &src, int effect, float value, const QColor &rgb, const QColor &rgb2, bool trans) const;
0124 
0125     /**
0126      * Applies an effect to a pixmap.
0127      * @param src The pixmap.
0128      * @param group The group for the icon, see KIconLoader::Group
0129      * @param state The icon's state, see KIconLoader::States
0130      * @return A pixmap with the effect applied.
0131      */
0132     QPixmap apply(const QPixmap &src, int group, int state) const;
0133 
0134     /**
0135      * Applies an effect to a pixmap.
0136      * @param src The pixmap.
0137      * @param effect The effect to apply, one of KIconEffect::Effects.
0138      * @param value Strength of the effect. 0 <= @p value <= 1.
0139      * @param rgb Color parameter for effects that need one.
0140      * @param trans Add Transparency if trans = true.
0141      * @return A pixmap with the effect applied.
0142      */
0143     QPixmap apply(const QPixmap &src, int effect, float value, const QColor &rgb, bool trans) const;
0144     QPixmap apply(const QPixmap &src, int effect, float value, const QColor &rgb, const QColor &rgb2, bool trans) const;
0145 
0146     /**
0147      * Returns an image twice as large, consisting of 2x2 pixels.
0148      * @param src the image.
0149      * @return the scaled image.
0150      */
0151     QImage doublePixels(const QImage &src) const;
0152 
0153     /**
0154      * Tints an image gray.
0155      *
0156      * @param image The image
0157      * @param value Strength of the effect. 0 <= @p value <= 1
0158      */
0159     static void toGray(QImage &image, float value);
0160 
0161     /**
0162      * Colorizes an image with a specific color.
0163      *
0164      * @param image The image
0165      * @param col The color with which the @p image is tinted
0166      * @param value Strength of the effect. 0 <= @p value <= 1
0167      */
0168     static void colorize(QImage &image, const QColor &col, float value);
0169 
0170     /**
0171      * Produces a monochrome icon with a given foreground and background color
0172      *
0173      * @param image The image
0174      * @param white The color with which the white parts of @p image are painted
0175      * @param black The color with which the black parts of @p image are painted
0176      * @param value Strength of the effect. 0 <= @p value <= 1
0177      */
0178     static void toMonochrome(QImage &image, const QColor &black, const QColor &white, float value);
0179 
0180     /**
0181      * Desaturates an image.
0182      *
0183      * @param image The image
0184      * @param value Strength of the effect. 0 <= @p value <= 1
0185      */
0186     static void deSaturate(QImage &image, float value);
0187 
0188     /**
0189      * Changes the gamma value of an image.
0190      *
0191      * @param image The image
0192      * @param value Strength of the effect. 0 <= @p value <= 1
0193      */
0194     static void toGamma(QImage &image, float value);
0195 
0196     /**
0197      * Renders an image semi-transparent.
0198      *
0199      * @param image The image
0200      */
0201     static void semiTransparent(QImage &image);
0202 
0203     /**
0204      * Renders a pixmap semi-transparent.
0205      *
0206      * @param pixmap The pixmap
0207      */
0208     static void semiTransparent(QPixmap &pixmap);
0209 
0210     /**
0211      * Overlays an image with an other image.
0212      *
0213      * @param src The image
0214      * @param overlay The image to overlay @p src with
0215      */
0216     static void overlay(QImage &src, QImage &overlay);
0217 
0218 private:
0219     std::unique_ptr<KIconEffectPrivate> const d;
0220 };
0221 
0222 #endif