/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/types.h>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <tools/color.hxx>
namespace
{
class Test:
public CppUnit::TestFixture
{
public :
void testVariables();
void test_asRGBColor();
void test_ApplyTintOrShade();
void test_ApplyLumModOff();
void testGetColorError();
void testInvert();
void testBColor();
void testLuminance();
void testCreateFromString();
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testVariables);
CPPUNIT_TEST(test_asRGBColor);
CPPUNIT_TEST(test_ApplyTintOrShade);
CPPUNIT_TEST(test_ApplyLumModOff);
CPPUNIT_TEST(testGetColorError);
CPPUNIT_TEST(testInvert);
CPPUNIT_TEST(testBColor);
CPPUNIT_TEST(testLuminance);
CPPUNIT_TEST(testCreateFromString);
CPPUNIT_TEST_SUITE_END();
};
void Test::testVariables()
{
Color aColor(
0 x44,
0 x88,
0 xAA);
CPPUNIT_ASSERT_EQUAL(
int (
0 x00),
int (
255 - aColor.GetAlpha()));
CPPUNIT_ASSERT_EQUAL(
int (
0 x44),
int (aColor.GetRed()));
CPPUNIT_ASSERT_EQUAL(
int (
0 x88),
int (aColor.GetGreen()));
CPPUNIT_ASSERT_EQUAL(
int (
0 xAA),
int (aColor.GetBlue()));
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x004488AA), sal_uInt32(aColor));
aColor = Color(ColorTransparency,
0 xAABBCCDD);
CPPUNIT_ASSERT_EQUAL(
int (
0 xAA),
int (
255 - aColor.GetAlpha()));
CPPUNIT_ASSERT_EQUAL(
int (
0 xBB),
int (aColor.GetRed()));
CPPUNIT_ASSERT_EQUAL(
int (
0 xCC),
int (aColor.GetGreen()));
CPPUNIT_ASSERT_EQUAL(
int (
0 xDD),
int (aColor.GetBlue()));
aColor.SetAlpha(
255 -
0 x11);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x11BBCCDD), sal_uInt32(aColor));
aColor.SetRed(
0 x22);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x1122CCDD), sal_uInt32(aColor));
aColor.SetGreen(
0 x33);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x112233DD), sal_uInt32(aColor));
aColor.SetBlue(
0 x44);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x11223344), sal_uInt32(aColor));
aColor.SetAlpha(
255 -
0 x77);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x77223344), sal_uInt32(aColor));
aColor.SetRed(
0 x88);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x77883344), sal_uInt32(aColor));
aColor.SetGreen(
0 x99);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x77889944), sal_uInt32(aColor));
aColor.SetBlue(
0 xAA);
CPPUNIT_ASSERT_EQUAL(sal_uInt32(
0 x778899AA), sal_uInt32(aColor));
}
void Test::test_asRGBColor()
{
Color aColor;
aColor = COL_BLACK;
CPPUNIT_ASSERT_EQUAL(u
"000000" _ustr, aColor.AsRGBHexString());
aColor = COL_WHITE;
CPPUNIT_ASSERT_EQUAL(u
"ffffff" _ustr, aColor.AsRGBHexString());
aColor = COL_RED;
CPPUNIT_ASSERT_EQUAL(u
"800000" _ustr, aColor.AsRGBHexString());
aColor = COL_TRANSPARENT;
CPPUNIT_ASSERT_EQUAL(u
"ffffff" _ustr, aColor.AsRGBHexString());
aColor = COL_BLUE;
CPPUNIT_ASSERT_EQUAL(u
"000080" _ustr, aColor.AsRGBHexString());
aColor.SetRed(
0 x12);
aColor.SetGreen(
0 x34);
aColor.SetBlue(
0 x56);
CPPUNIT_ASSERT_EQUAL(u
"123456" _ustr, aColor.AsRGBHexString());
aColor = COL_AUTO;
CPPUNIT_ASSERT_EQUAL(u
"ffffff" _ustr, aColor.AsRGBHexString());
}
OUString createTintShade(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB, std::u16string_view s
Reference, sal_Int16 nTintShade)
{
Color aColor(nR, nG, nB);
if (sReference != aColor.AsRGBHexString())
return OUString();
aColor.ApplyTintOrShade(nTintShade);
return aColor.AsRGBHexString();
}
void Test::test_ApplyTintOrShade()
{
// BLACK reference
// 5% tint
CPPUNIT_ASSERT_EQUAL(u"0d0d0d" _ustr, createTintShade(0 x00, 0 x00, 0 x00, u"000000" , 500 ));
// 15% tint
CPPUNIT_ASSERT_EQUAL(u"262626" _ustr, createTintShade(0 x00, 0 x00, 0 x00, u"000000" , 1500 ));
// 25% tint
CPPUNIT_ASSERT_EQUAL(u"404040" _ustr, createTintShade(0 x00, 0 x00, 0 x00, u"000000" , 2500 ));
// 50% tint
CPPUNIT_ASSERT_EQUAL(u"808080" _ustr, createTintShade(0 x00, 0 x00, 0 x00, u"000000" , 5000 ));
// 100% tint
CPPUNIT_ASSERT_EQUAL(u"ffffff" _ustr, createTintShade(0 x00, 0 x00, 0 x00, u"000000" , 10000 ));
// WHITE reference
// 5% shade
CPPUNIT_ASSERT_EQUAL(u"f2f2f2" _ustr, createTintShade(0 xff, 0 xff, 0 xff, u"ffffff" , -500 ));
// 15% shade
CPPUNIT_ASSERT_EQUAL(u"d9d9d9" _ustr, createTintShade(0 xff, 0 xff, 0 xff, u"ffffff" , -1500 ));
// 25% shade
CPPUNIT_ASSERT_EQUAL(u"bfbfbf" _ustr, createTintShade(0 xff, 0 xff, 0 xff, u"ffffff" , -2500 ));
// 50% shade
CPPUNIT_ASSERT_EQUAL(u"808080" _ustr, createTintShade(0 xff, 0 xff, 0 xff, u"ffffff" , -5000 ));
// 100% shade
CPPUNIT_ASSERT_EQUAL(u"000000" _ustr, createTintShade(0 xff, 0 xff, 0 xff, u"ffffff" , -10000 ));
// GREY reference
// 0% - no change
CPPUNIT_ASSERT_EQUAL(u"808080" _ustr, createTintShade(0 x80, 0 x80, 0 x80, u"808080" , 0 ));
// 25% tint
CPPUNIT_ASSERT_EQUAL(u"a0a0a0" _ustr, createTintShade(0 x80, 0 x80, 0 x80, u"808080" , 2500 ));
// 50% tint
//CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
// disable for now - a rounding error happens on come platforms...
// 100% tint
CPPUNIT_ASSERT_EQUAL(u"ffffff" _ustr, createTintShade(0 x80, 0 x80, 0 x80, u"808080" , 10000 ));
// 25% shade
CPPUNIT_ASSERT_EQUAL(u"606060" _ustr, createTintShade(0 x80, 0 x80, 0 x80, u"808080" , -2500 ));
// 50% shade
CPPUNIT_ASSERT_EQUAL(u"404040" _ustr, createTintShade(0 x80, 0 x80, 0 x80, u"808080" , -5000 ));
// 100% shade
CPPUNIT_ASSERT_EQUAL(u"000000" _ustr, createTintShade(0 x80, 0 x80, 0 x80, u"808080" , -10000 ));
}
void Test::test_ApplyLumModOff()
{
// Kind of blue.
Color aColor(0 x44, 0 x72, 0 xC4);
// PowerPoint calls this "Lighter 40%".
aColor.ApplyLumModOff(6000 , 4000 );
CPPUNIT_ASSERT_EQUAL(u"8faadc" _ustr, aColor.AsRGBHexString());
}
void Test::testGetColorError()
{
CPPUNIT_ASSERT_EQUAL(sal_uInt16(0 ), Color(0 xAA, 0 xBB, 0 xCC).GetColorError(Color(0 xAA, 0 xBB, 0 xCC)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(1 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA1, 0 xB0, 0 xC0)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(1 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA0, 0 xB1, 0 xC0)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(1 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA0, 0 xB0, 0 xC1)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(2 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA1, 0 xB1, 0 xC0)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(2 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA0, 0 xB1, 0 xC1)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(2 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA1, 0 xB0, 0 xC1)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(3 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA1, 0 xB1, 0 xC1)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(3 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA1, 0 xB1, 0 xC1)));
CPPUNIT_ASSERT_EQUAL(sal_uInt16(3 ), Color(0 xA0, 0 xB0, 0 xC0).GetColorError(Color(0 xA1, 0 xB1, 0 xC1)));
}
void Test::testInvert()
{
Color aColor(0 xFF, 0 x00, 0 x88);
aColor.Invert();
CPPUNIT_ASSERT_EQUAL(Color(0 x00, 0 xFF, 0 x77).AsRGBHexString(), aColor.AsRGBHexString());
// Alpha should be unaffected
aColor = Color(ColorTransparency, 0 x22, 0 xFF, 0 x00, 0 x88);
aColor.Invert();
CPPUNIT_ASSERT_EQUAL(Color(ColorTransparency, 0 x22, 0 x00, 0 xFF, 0 x77).AsRGBHexString(), aColor.AsRGBHexString());
}
void Test::testBColor()
{
Color aColor;
aColor = Color(basegfx::BColor(0 .0 , 0 .0 , 0 .0 ));
CPPUNIT_ASSERT_EQUAL(Color(0 x00, 0 x00, 0 x00).AsRGBHexString(), aColor.AsRGBHexString());
CPPUNIT_ASSERT_EQUAL(0 .0 , aColor.getBColor().getRed());
CPPUNIT_ASSERT_EQUAL(0 .0 , aColor.getBColor().getGreen());
CPPUNIT_ASSERT_EQUAL(0 .0 , aColor.getBColor().getBlue());
aColor = Color(basegfx::BColor(1 .0 , 1 .0 , 1 .0 ));
CPPUNIT_ASSERT_EQUAL(Color(0 xFF, 0 xFF, 0 xFF).AsRGBHexString(), aColor.AsRGBHexString());
CPPUNIT_ASSERT_EQUAL(1 .0 , aColor.getBColor().getRed());
CPPUNIT_ASSERT_EQUAL(1 .0 , aColor.getBColor().getGreen());
CPPUNIT_ASSERT_EQUAL(1 .0 , aColor.getBColor().getBlue());
aColor = Color(basegfx::BColor(0 .5 , 0 .25 , 0 .125 ));
CPPUNIT_ASSERT_EQUAL(Color(0 x80, 0 x40, 0 x20).AsRGBHexString(), aColor.AsRGBHexString());
// FP error is rather big, but that's normal
CPPUNIT_ASSERT_DOUBLES_EQUAL(0 .500 , aColor.getBColor().getRed(), 1 E-2 );
CPPUNIT_ASSERT_DOUBLES_EQUAL(0 .250 , aColor.getBColor().getGreen(), 1 E-2 );
CPPUNIT_ASSERT_DOUBLES_EQUAL(0 .125 , aColor.getBColor().getBlue(), 1 E-2 );
}
void Test::testLuminance()
{
CPPUNIT_ASSERT_EQUAL(sal_uInt8(0 ), COL_BLACK.GetLuminance());
CPPUNIT_ASSERT_EQUAL(sal_uInt8(255 ), COL_WHITE.GetLuminance());
CPPUNIT_ASSERT_EQUAL(sal_uInt8(128 ), Color(128 , 128 , 128 ).GetLuminance());
CPPUNIT_ASSERT(COL_WHITE.IsBright());
CPPUNIT_ASSERT(COL_BLACK.IsDark());
CPPUNIT_ASSERT(Color(249 , 250 , 251 ).IsBright());
CPPUNIT_ASSERT(Color(9 , 10 , 11 ).IsDark());
CPPUNIT_ASSERT(COL_WHITE.GetLuminance() > COL_BLACK.GetLuminance());
}
void Test::testCreateFromString()
{
Color aColor;
CPPUNIT_ASSERT(color::createFromString("#00FF00" _ostr, aColor));
CPPUNIT_ASSERT_EQUAL(Color(0 x00, 0 xff, 0 x00), aColor);
}
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Messung V0.5 in Prozent C=93 H=95 G=93
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-06)
¤
*© Formatika GbR, Deutschland