include/tools/color.hxx | 5 +++++ tools/qa/cppunit/test_color.cxx | 9 +++++++++ tools/source/generic/color.cxx | 21 +++++++++++++++++++++ vcl/source/gdi/WidgetDefinitionReader.cxx | 25 ++++--------------------- 4 files changed, 39 insertions(+), 21 deletions(-)
New commits: commit b9477183bb383c7a00d5ad2c27985fbd9d22173b Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Fri Jan 17 16:07:37 2025 +0900 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Mon Jan 20 01:40:10 2025 +0100 tools: moved function to create the color from string to tools Will be needed again in a follow up commit. Change-Id: Idbe2f540f9c3fb974f7dadf62dc099a0791bb0c6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180378 Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> Tested-by: Jenkins diff --git a/include/tools/color.hxx b/include/tools/color.hxx index 5d2052e297a4..16c1a6259f8e 100644 --- a/include/tools/color.hxx +++ b/include/tools/color.hxx @@ -480,6 +480,11 @@ inline std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT, return rStream; } +namespace color +{ +TOOLS_DLLPUBLIC bool createFromString(OString const& rString, Color& rColor); +} + #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/qa/cppunit/test_color.cxx b/tools/qa/cppunit/test_color.cxx index 4e2046c58e71..736897a81ef7 100644 --- a/tools/qa/cppunit/test_color.cxx +++ b/tools/qa/cppunit/test_color.cxx @@ -27,6 +27,7 @@ public: void testInvert(); void testBColor(); void testLuminance(); + void testCreateFromString(); CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testVariables); @@ -37,6 +38,7 @@ public: CPPUNIT_TEST(testInvert); CPPUNIT_TEST(testBColor); CPPUNIT_TEST(testLuminance); + CPPUNIT_TEST(testCreateFromString); CPPUNIT_TEST_SUITE_END(); }; @@ -245,6 +247,13 @@ void Test::testLuminance() CPPUNIT_ASSERT(COL_WHITE.GetLuminance() > COL_BLACK.GetLuminance()); } +void Test::testCreateFromString() +{ + Color aColor; + CPPUNIT_ASSERT(color::createFromString("#00FF00"_ostr, aColor)); + CPPUNIT_ASSERT_EQUAL(Color(0x00, 0xff, 0x00), aColor); +} + CPPUNIT_TEST_SUITE_REGISTRATION(Test); } diff --git a/tools/source/generic/color.cxx b/tools/source/generic/color.cxx index 0834b0946722..3a43c95d948b 100644 --- a/tools/source/generic/color.cxx +++ b/tools/source/generic/color.cxx @@ -28,6 +28,7 @@ #include <tools/helpers.hxx> #include <tools/long.hxx> #include <o3tl/string_view.hxx> +#include <o3tl/numeric.hxx> #include <basegfx/color/bcolortools.hxx> #include <basegfx/numeric/ftools.hxx> @@ -295,4 +296,24 @@ void Color::ApplyLumModOff(sal_Int16 nMod, sal_Int16 nOff) B = sal_uInt8(std::lround(aBColor.getBlue() * 255.0)); } +namespace color +{ +bool createFromString(OString const& rString, Color& rColor) +{ + if (rString.getLength() != 7) + return false; + + const char aChar(rString[0]); + + if (aChar != '#') + return false; + + rColor.SetRed(o3tl::convertToHex<sal_Int32>(rString[1], rString[2])); + rColor.SetGreen(o3tl::convertToHex<sal_Int32>(rString[3], rString[4])); + rColor.SetBlue(o3tl::convertToHex<sal_Int32>(rString[5], rString[6])); + + return true; +} +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/gdi/WidgetDefinitionReader.cxx b/vcl/source/gdi/WidgetDefinitionReader.cxx index 8e80ab785f46..24585bea784b 100644 --- a/vcl/source/gdi/WidgetDefinitionReader.cxx +++ b/vcl/source/gdi/WidgetDefinitionReader.cxx @@ -29,23 +29,6 @@ bool lcl_fileExists(OUString const& sFilename) return osl::FileBase::E_None == eRC; } -bool readColor(OString const& rString, Color& rColor) -{ - if (rString.getLength() != 7) - return false; - - const char aChar(rString[0]); - - if (aChar != '#') - return false; - - rColor.SetRed(o3tl::convertToHex<sal_Int32>(rString[1], rString[2])); - rColor.SetGreen(o3tl::convertToHex<sal_Int32>(rString[3], rString[4])); - rColor.SetBlue(o3tl::convertToHex<sal_Int32>(rString[5], rString[6])); - - return true; -} - bool readSetting(OString const& rInputString, OString& rOutputString) { if (!rInputString.isEmpty()) @@ -191,9 +174,9 @@ void WidgetDefinitionReader::readDrawingDefinition( if (rWalker.name() == "rect") { Color aStrokeColor; - readColor(rWalker.attribute("stroke"_ostr), aStrokeColor); + color::createFromString(rWalker.attribute("stroke"_ostr), aStrokeColor); Color aFillColor; - readColor(rWalker.attribute("fill"_ostr), aFillColor); + color::createFromString(rWalker.attribute("fill"_ostr), aFillColor); OString sStrokeWidth = rWalker.attribute("stroke-width"_ostr); sal_Int32 nStrokeWidth = -1; if (!sStrokeWidth.isEmpty()) @@ -227,7 +210,7 @@ void WidgetDefinitionReader::readDrawingDefinition( else if (rWalker.name() == "line") { Color aStrokeColor; - readColor(rWalker.attribute("stroke"_ostr), aStrokeColor); + color::createFromString(rWalker.attribute("stroke"_ostr), aStrokeColor); OString sStrokeWidth = rWalker.attribute("stroke-width"_ostr); sal_Int32 nStrokeWidth = -1; @@ -448,7 +431,7 @@ bool WidgetDefinitionReader::read(WidgetDefinition& rWidgetDefinition) auto pair = aStyleColorMap.find(aWalker.name()); if (pair != aStyleColorMap.end()) { - readColor(aWalker.attribute("value"_ostr), *pair->second); + color::createFromString(aWalker.attribute("value"_ostr), *pair->second); } aWalker.next(); }