summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorThomas Hartmann <Thomas.Hartmann@nokia.com>2009-08-24 09:02:27 (GMT)
committerThomas Hartmann <Thomas.Hartmann@nokia.com>2009-08-24 09:52:30 (GMT)
commitd1111859c26526227c07793aa6747efa65496055 (patch)
treed3189b11b67ccd679a076cd5f9aaea8d2bd8bddd /tests/auto
parent8260b28cbbc1103a20e9f920fad099922021e9c7 (diff)
downloadQt-d1111859c26526227c07793aa6747efa65496055.zip
Qt-d1111859c26526227c07793aa6747efa65496055.tar.gz
Qt-d1111859c26526227c07793aa6747efa65496055.tar.bz2
HSL support for QColor
This patch adds support for the HSL color space to QColor See discussions on dev mailing list Reviewed-by: Marco Bubke
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/qcolor/tst_qcolor.cpp230
1 files changed, 217 insertions, 13 deletions
diff --git a/tests/auto/qcolor/tst_qcolor.cpp b/tests/auto/qcolor/tst_qcolor.cpp
index bc0901a..b61c98d 100644
--- a/tests/auto/qcolor/tst_qcolor.cpp
+++ b/tests/auto/qcolor/tst_qcolor.cpp
@@ -109,6 +109,13 @@ private slots:
void getCmyk();
void setCmyk();
+ void hueHsl();
+ void saturationHsl();
+ void lightness();
+
+ void getHsl();
+ void setHsl();
+
void toRgb_data();
void toRgb();
void toRgbNonDestructive();
@@ -121,11 +128,16 @@ private slots:
void toCmyk();
void toCmykNonDestructive();
+ void toHsl_data();
+ void toHsl();;
+ void toHslNonDestructive();
+
void convertTo();
void fromRgb();
void fromHsv();
void fromCmyk();
+ void fromHsl();
void light();
void dark();
@@ -556,6 +568,10 @@ void tst_QColor::spec()
QColor cmyk = QColor::fromCmyk(0, 0, 0, 0);
QCOMPARE(cmyk.spec(), QColor::Cmyk);
+
+ QColor hsl = QColor::fromHsl(0, 0, 0, 0);
+ QCOMPARE(hsl.spec(), QColor::Hsl);
+
}
void tst_QColor::alpha()
@@ -1044,56 +1060,179 @@ void tst_QColor::setCmyk()
}
}
+void tst_QColor::hueHsl()
+{ DEPENDS_ON(setHsl()); }
+
+void tst_QColor::saturationHsl()
+{ DEPENDS_ON(setHsl()); }
+
+void tst_QColor::lightness()
+{ DEPENDS_ON(setHsl()); }
+
+void tst_QColor::getHsl()
+{ DEPENDS_ON(setHsl()); }
+
+void tst_QColor::setHsl()
+{
+ QColor color;
+
+ for (int A = 0; A <= USHRT_MAX; ++A) {
+ {
+ // 0-255
+ int a = A >> 8;
+ color.setHsl(0, 0, 0, a);
+ QCOMPARE(color.alpha(), a);
+
+ int h, s, l, a2;
+ color.getHsv(&h, &s, &l, &a2);
+ QCOMPARE(a2, a);
+ }
+
+ {
+ // 0.0-1.0
+ qreal a = A / qreal(USHRT_MAX);
+ color.setHslF(0.0, 0.0, 0.0, a); QCOMPARE(color.alphaF(), a);
+
+ qreal h, s, l, a2;
+ color.getHslF(&h, &s, &l, &a2);
+ QCOMPARE(a2, a);
+ }
+ }
+
+ for (int H = 0; H < 36000; ++H) {
+ {
+ // 0-255
+ int h = H / 100;
+
+ color.setHsl(h, 0, 0, 0);
+ QCOMPARE(color.hslHue(), h);
+
+ int h2, s, l, a;
+ color.getHsl(&h2, &s, &l, &a);
+ QCOMPARE(h2, h);
+ }
+
+ {
+ // 0.0-1.0
+ qreal h = H / 36000.0;
+ color.setHslF(h, 0.0, 0.0, 0.0);
+ QCOMPARE(color.hslHueF(), h);
+
+ qreal h2, s, l, a;
+ color.getHslF(&h2, &s, &l, &a);
+ QCOMPARE(h2, h);
+ }
+ }
+
+ for (int S = 0; S <= USHRT_MAX; ++S) {
+ {
+ // 0-255
+ int s = S >> 8;
+ color.setHsl(0, s, 0, 0);
+ QCOMPARE(color.hslSaturation(), s);
+
+ int h, s2, l, a;
+ color.getHsl(&h, &s2, &l, &a);
+ QCOMPARE(s2, s);
+ }
+
+ {
+ // 0.0-1.0
+ qreal s = S / qreal(USHRT_MAX);
+ color.setHslF(0.0, s, 0.0, 0.0);
+ QCOMPARE(color.hslSaturationF(), s);
+
+ qreal h, s2, l, a;
+ color.getHslF(&h, &s2, &l, &a);
+ QCOMPARE(s2, s);
+ }
+ }
+
+ for (int L = 0; L <= USHRT_MAX; ++L) {
+ {
+ // 0-255
+ int l = L >> 8;
+ color.setHsl(0, 0, l, 0);
+ QCOMPARE(color.lightness(), l);
+
+ int h, s, l2, a;
+ color.getHsl(&h, &s, &l2, &a);
+ QCOMPARE(l2, l);
+ }
+
+ {
+ // 0.0-1.0
+ qreal l = L / qreal(USHRT_MAX);
+ color.setHslF(0.0, 0.0, l, 0.0);
+ QCOMPARE(color.lightnessF(), l);
+
+ qreal h, s, l2, a;
+ color.getHslF(&h, &s, &l2, &a);
+ QCOMPARE(l2, l);
+ }
+ }
+}
+
void tst_QColor::toRgb_data()
{
QTest::addColumn<QColor>("expectedColor");
QTest::addColumn<QColor>("hsvColor");
QTest::addColumn<QColor>("cmykColor");
+ QTest::addColumn<QColor>("hslColor");
QTest::newRow("black")
<< QColor::fromRgbF(0.0, 0.0, 0.0)
<< QColor::fromHsvF(-1.0, 0.0, 0.0)
- << QColor::fromCmykF(0.0, 0.0, 0.0, 1.0);
+ << QColor::fromCmykF(0.0, 0.0, 0.0, 1.0)
+ << QColor::fromHslF(-1.0, 0.0, 0.0);
QTest::newRow("white")
<< QColor::fromRgbF(1.0, 1.0, 1.0)
<< QColor::fromHsvF(-1.0, 0.0, 1.0)
- << QColor::fromCmykF(0.0, 0.0, 0.0, 0.0);
+ << QColor::fromCmykF(0.0, 0.0, 0.0, 0.0)
+ << QColor::fromHslF(-1.0, 0.0, 1.0);
QTest::newRow("red")
<< QColor::fromRgbF(1.0, 0.0, 0.0)
<< QColor::fromHsvF(0.0, 1.0, 1.0)
- << QColor::fromCmykF(0.0, 1.0, 1.0, 0.0);
+ << QColor::fromCmykF(0.0, 1.0, 1.0, 0.0)
+ << QColor::fromHslF(0.0, 1.0, 0.5, 1.0);
QTest::newRow("green")
<< QColor::fromRgbF(0.0, 1.0, 0.0)
<< QColor::fromHsvF(0.33333, 1.0, 1.0)
- << QColor::fromCmykF(1.0, 0.0, 1.0, 0.0);
+ << QColor::fromCmykF(1.0, 0.0, 1.0, 0.0)
+ << QColor::fromHslF(0.33333, 1.0, 0.5);
QTest::newRow("blue")
<< QColor::fromRgbF(0.0, 0.0, 1.0)
<< QColor::fromHsvF(0.66667, 1.0, 1.0)
- << QColor::fromCmykF(1.0, 1.0, 0.0, 0.0);
+ << QColor::fromCmykF(1.0, 1.0, 0.0, 0.0)
+ << QColor::fromHslF(0.66667, 1.0, 0.5);
QTest::newRow("cyan")
<< QColor::fromRgbF(0.0, 1.0, 1.0)
<< QColor::fromHsvF(0.5, 1.0, 1.0)
- << QColor::fromCmykF(1.0, 0.0, 0.0, 0.0);
+ << QColor::fromCmykF(1.0, 0.0, 0.0, 0.0)
+ << QColor::fromHslF(0.5, 1.0, 0.5);
QTest::newRow("magenta")
<< QColor::fromRgbF(1.0, 0.0, 1.0)
<< QColor::fromHsvF(0.83333, 1.0, 1.0)
- << QColor::fromCmykF(0.0, 1.0, 0.0, 0.0);
+ << QColor::fromCmykF(0.0, 1.0, 0.0, 0.0)
+ << QColor::fromHslF(0.83333, 1.0, 0.5);
QTest::newRow("yellow")
<< QColor::fromRgbF(1.0, 1.0, 0.0)
<< QColor::fromHsvF(0.16667, 1.0, 1.0)
- << QColor::fromCmykF(0.0, 0.0, 1.0, 0.0);
+ << QColor::fromCmykF(0.0, 0.0, 1.0, 0.0)
+ << QColor::fromHslF(0.16667, 1.0, 0.5);
QTest::newRow("gray")
<< QColor::fromRgbF(0.6431375, 0.6431375, 0.6431375)
<< QColor::fromHsvF(-1.0, 0.0, 0.6431375)
- << QColor::fromCmykF(0.0, 0.0, 0.0, 0.356863);
+ << QColor::fromCmykF(0.0, 0.0, 0.0, 0.356863)
+ << QColor::fromHslF(-1.0, 0.0, 0.6431375);
// ### add colors using the 0-255 functions
}
@@ -1106,8 +1245,11 @@ void tst_QColor::toRgb()
QFETCH(QColor, expectedColor);
QFETCH(QColor, hsvColor);
QFETCH(QColor, cmykColor);
+ QFETCH(QColor, hslColor);
QCOMPARE(hsvColor.toRgb(), expectedColor);
QCOMPARE(cmykColor.toRgb(), expectedColor);
+ QCOMPARE(hslColor.toRgb(), expectedColor);
+
}
void tst_QColor::toHsv_data()
@@ -1115,16 +1257,19 @@ void tst_QColor::toHsv_data()
QTest::addColumn<QColor>("expectedColor");
QTest::addColumn<QColor>("rgbColor");
QTest::addColumn<QColor>("cmykColor");
+ QTest::addColumn<QColor>("hslColor");
QTest::newRow("data0")
<< QColor::fromHsv(300, 255, 255)
<< QColor(255, 0, 255)
- << QColor::fromCmyk(0, 255, 0, 0);
+ << QColor::fromCmyk(0, 255, 0, 0)
+ << QColor::fromHslF(300./360., 1., 0.5, 1.0);
QTest::newRow("data1")
<< QColor::fromHsvF(1., 1., 1., 1.)
<< QColor(255, 0, 0, 255)
- << QColor::fromCmykF(0., 1., 1., 0.);
+ << QColor::fromCmykF(0., 1., 1., 0.)
+ << QColor::fromHsvF(1., 1., 1., 1.);
}
void tst_QColor::toRgbNonDestructive()
@@ -1141,8 +1286,10 @@ void tst_QColor::toHsv()
QFETCH(QColor, expectedColor);
QFETCH(QColor, rgbColor);
QFETCH(QColor, cmykColor);
+ QFETCH(QColor, hslColor);
QCOMPARE(rgbColor.toHsv(), expectedColor);
QCOMPARE(cmykColor.toHsv(), expectedColor);
+ QCOMPARE(hslColor.toHsv(), expectedColor);
}
void tst_QColor::toHsvNonDestructive()
@@ -1156,16 +1303,19 @@ void tst_QColor::toCmyk_data()
QTest::addColumn<QColor>("expectedColor");
QTest::addColumn<QColor>("rgbColor");
QTest::addColumn<QColor>("hsvColor");
+ QTest::addColumn<QColor>("hslColor");
QTest::newRow("data0")
<< QColor::fromCmykF(1.0, 0.0, 0.0, 0.0)
<< QColor(0, 255, 255)
- << QColor::fromHsv(180, 255, 255);
+ << QColor::fromHsv(180, 255, 255)
+ << QColor::fromHslF(180./360., 1., 0.5, 1.0);
QTest::newRow("data1")
<< QColor::fromCmyk(255, 255, 255, 255)
<< QColor::fromRgb(0, 0, 0)
- << QColor::fromRgb(0, 0, 0).toHsv();
+ << QColor::fromRgb(0, 0, 0).toHsv()
+ << QColor::fromRgb(0, 0, 0).toHsl();
}
void tst_QColor::toCmyk()
@@ -1176,8 +1326,10 @@ void tst_QColor::toCmyk()
QFETCH(QColor, expectedColor);
QFETCH(QColor, rgbColor);
QFETCH(QColor, hsvColor);
+ QFETCH(QColor, hslColor);
QCOMPARE(rgbColor.toHsv().toCmyk(), expectedColor);
QCOMPARE(hsvColor.toCmyk(), expectedColor);
+ QCOMPARE(hslColor.toCmyk(), expectedColor);
}
void tst_QColor::toCmykNonDestructive()
@@ -1186,6 +1338,51 @@ void tst_QColor::toCmykNonDestructive()
QCOMPARE(aColor, aColor.toCmyk());
}
+void tst_QColor::toHsl_data()
+{
+ QTest::addColumn<QColor>("expectedColor");
+ QTest::addColumn<QColor>("hsvColor");
+ QTest::addColumn<QColor>("rgbColor");
+ QTest::addColumn<QColor>("cmykColor");
+
+
+ QTest::newRow("data0")
+ << QColor::fromHslF(300./360., 1., 0.5, 1.0)
+ << QColor::fromHsv(300, 255, 255)
+ << QColor(255, 0, 255)
+ << QColor::fromCmyk(0, 255, 0, 0);
+
+ QTest::newRow("data1")
+ << QColor::fromHslF(1., 1., 0.5, 1.0)
+ << QColor::fromHsvF(1., 1., 1., 1.)
+ << QColor(255, 0, 0, 255)
+ << QColor::fromCmykF(0., 1., 1., 0.);
+}
+
+void tst_QColor::toHsl()
+{
+ // invalid should remain invalid
+ QVERIFY(!QColor().toHsl().isValid());
+
+ QFETCH(QColor, expectedColor);
+ QFETCH(QColor, rgbColor);
+ QFETCH(QColor, cmykColor);
+ QFETCH(QColor, hsvColor);
+
+ QCOMPARE(rgbColor.toHsl(), expectedColor);
+ QCOMPARE(cmykColor.toHsl(), expectedColor);
+ QCOMPARE(hsvColor.toHsl(), expectedColor);
+
+}
+
+
+void tst_QColor::toHslNonDestructive()
+{
+ QColor aColor = QColor::fromHslF(0.11, 0.22, 0.33, 0.44);
+ QCOMPARE(aColor, aColor.toHsl());
+}
+
+
void tst_QColor::convertTo()
{
QColor color(Qt::black);
@@ -1199,12 +1396,16 @@ void tst_QColor::convertTo()
QColor cmyk = color.convertTo(QColor::Cmyk);
QVERIFY(cmyk.spec() == QColor::Cmyk);
+ QColor hsl = color.convertTo(QColor::Hsl);
+ QVERIFY(hsl.spec() == QColor::Hsl);
+
QColor invalid = color.convertTo(QColor::Invalid);
QVERIFY(invalid.spec() == QColor::Invalid);
DEPENDS_ON(toRgb());
DEPENDS_ON(toHsv());
DEPENDS_ON(toCmyk());
+ DEPENDS_ON(toHsl());
}
void tst_QColor::fromRgb()
@@ -1216,6 +1417,9 @@ void tst_QColor::fromHsv()
void tst_QColor::fromCmyk()
{ DEPENDS_ON(convertTo()); }
+void tst_QColor::fromHsl()
+{ DEPENDS_ON(convertTo()); }
+
void tst_QColor::light()
{
QColor gray(Qt::gray);