File indexing completed on 2024-04-28 05:32:10
0001 #ifndef oxygencolorutils_h 0002 #define oxygencolorutils_h 0003 /* 0004 * this file is part of the oxygen gtk engine 0005 * SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr> 0006 * 0007 * inspired notably from kdelibs/kdeui/color/kcolorutils.h 0008 * SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net> 0009 * SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org> 0010 * SPDX-FileCopyrightText: 2007 Zack Rusin <zack@kde.org> 0011 * 0012 * SPDX-License-Identifier: LGPL-2.0-or-later 0013 */ 0014 0015 #include "oxygenrgba.h" 0016 0017 namespace Oxygen 0018 { 0019 namespace ColorUtils 0020 { 0021 0022 //! set contrast values 0023 void setContrast( double ); 0024 0025 //! clear caches 0026 void clearCaches( void ); 0027 0028 //! contrast 0029 const double& contrast( void ); 0030 0031 //! background contrast 0032 const double& backgroundContrast( void ); 0033 0034 //!@name color utilities 0035 //@{ 0036 bool lowThreshold( const Rgba& ); 0037 bool highThreshold( const Rgba& ); 0038 Rgba backgroundTopColor( const Rgba& ); 0039 Rgba backgroundBottomColor( const Rgba& ); 0040 Rgba backgroundRadialColor( const Rgba& ); 0041 Rgba lightColor( const Rgba& ); 0042 Rgba darkColor( const Rgba& ); 0043 Rgba midColor( const Rgba& ); 0044 Rgba shadowColor( const Rgba& ); 0045 0046 //! decoration color. Merges foreground and background 0047 Rgba decoColor( const Rgba& background, const Rgba& color ); 0048 0049 //! change alpha chanel (multiplicative) 0050 Rgba alphaColor( const Rgba& color, double alpha); 0051 0052 //! returns background color matching position in a top level widget of given height 0053 Rgba backgroundColor(const Rgba &color, double ratio); 0054 0055 //! returns background color matching position in a top level widget of given height 0056 inline Rgba backgroundColor(const Rgba &color, int height, int y) 0057 { return (height > 0) ? backgroundColor( color, std::min( 1.0 , double( y )/std::min( 300, 3*height/4 ) ) ) : color; } 0058 0059 //! returns menu background color matching position in a top level widget of given height 0060 inline Rgba menuBackgroundColor(const Rgba &color, int height, int y) 0061 { return (height > 0) ? backgroundColor( color, std::min( 1.0, double( y )/std::min( 200, 3*height/4 ) ) ) : color; } 0062 0063 //@} 0064 0065 /*! 0066 Calculate the luma of a color. Luma is weighted sum of gamma-adjusted 0067 R'G'B' components of a color. The result is similar to qGray. The range 0068 is from 0.0 (black) to 1.0 (white). 0069 */ 0070 double luma(const Rgba&); 0071 0072 /*! 0073 Calculate the contrast ratio between two colors, according to the 0074 W3C/WCAG2.0 algorithm, (Lmax + 0.05)/(Lmin + 0.05), where Lmax and Lmin 0075 are the luma values of the lighter color and the darker color, 0076 respectively. 0077 */ 0078 double contrastRatio(const Rgba&, const Rgba&); 0079 0080 //! Adjust the luma of a color by changing its distance from white. 0081 Rgba lighten(const Rgba&, double amount = 0.5, double chromaInverseGain = 1.0); 0082 0083 //! Adjust the luma of a color by changing its distance from black. 0084 Rgba darken(const Rgba&, double amount = 0.5, double chromaGain = 1.0); 0085 0086 //! Create a new color by tinting one color with another 0087 Rgba tint(const Rgba &base, const Rgba &color, double amount = 0.3); 0088 0089 //! mix two colors 0090 Rgba mix(const Rgba &c1, const Rgba &c2, double bias = 0.5); 0091 0092 enum ShadeRole 0093 { 0094 0095 // The light color is lighter than dark() or shadow() and contrasts with the base color. 0096 LightShade, 0097 0098 // The midlight color is in between base() and light(). 0099 MidlightShade, 0100 0101 // The mid color is in between base() and dark(). 0102 MidShade, 0103 0104 // The dark color is in between mid() and shadow(). 0105 DarkShade, 0106 0107 // The shadow color is darker than light() or midlight() and contrasts the base color. 0108 ShadowShade 0109 0110 }; 0111 0112 Rgba shade(const Rgba&, ShadeRole); 0113 Rgba shade(const Rgba&, ShadeRole, double contrast, double chromaAdjust = 0.0); 0114 Rgba shade(const Rgba&, double lumaAmount, double chromaAmount = 0.0); 0115 0116 } 0117 } 0118 0119 #endif