00001
00022 #include "color_utils.hpp"
00023
00024 namespace color_widgets {
00025 namespace detail {
00026
00027 QColor color_from_lch(qreal hue, qreal chroma, qreal luma, qreal alpha )
00028 {
00029 qreal h1 = hue*6;
00030 qreal x = chroma*(1-qAbs(std::fmod(h1,2)-1));
00031 QColor col;
00032 if ( h1 >= 0 && h1 < 1 )
00033 col = QColor::fromRgbF(chroma,x,0);
00034 else if ( h1 < 2 )
00035 col = QColor::fromRgbF(x,chroma,0);
00036 else if ( h1 < 3 )
00037 col = QColor::fromRgbF(0,chroma,x);
00038 else if ( h1 < 4 )
00039 col = QColor::fromRgbF(0,x,chroma);
00040 else if ( h1 < 5 )
00041 col = QColor::fromRgbF(x,0,chroma);
00042 else if ( h1 < 6 )
00043 col = QColor::fromRgbF(chroma,0,x);
00044
00045 qreal m = luma - color_lumaF(col);
00046
00047 return QColor::fromRgbF(
00048 qBound(0.0,col.redF()+m,1.0),
00049 qBound(0.0,col.greenF()+m,1.0),
00050 qBound(0.0,col.blueF()+m,1.0),
00051 alpha);
00052 }
00053
00054 QColor color_from_hsl(qreal hue, qreal sat, qreal lig, qreal alpha )
00055 {
00056 qreal chroma = (1 - qAbs(2*lig-1))*sat;
00057 qreal h1 = hue*6;
00058 qreal x = chroma*(1-qAbs(std::fmod(h1,2)-1));
00059 QColor col;
00060 if ( h1 >= 0 && h1 < 1 )
00061 col = QColor::fromRgbF(chroma,x,0);
00062 else if ( h1 < 2 )
00063 col = QColor::fromRgbF(x,chroma,0);
00064 else if ( h1 < 3 )
00065 col = QColor::fromRgbF(0,chroma,x);
00066 else if ( h1 < 4 )
00067 col = QColor::fromRgbF(0,x,chroma);
00068 else if ( h1 < 5 )
00069 col = QColor::fromRgbF(x,0,chroma);
00070 else if ( h1 < 6 )
00071 col = QColor::fromRgbF(chroma,0,x);
00072
00073 qreal m = lig-chroma/2;
00074
00075 return QColor::fromRgbF(
00076 qBound(0.0,col.redF()+m,1.0),
00077 qBound(0.0,col.greenF()+m,1.0),
00078 qBound(0.0,col.blueF()+m,1.0),
00079 alpha);
00080 }
00081
00082 }
00083 }