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