Kaydet (Commit) 599328b0 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl

tools: add AsRGBHexString to Color + unit test

AsRGBHexString returns the Color as a RGB hex string. For example
"00ff00" for green color.

Change-Id: Ia95c7f9eb6d9aefc3ca989046fa8d76b7b7f9e8f
üst 1c77fcac
...@@ -162,6 +162,10 @@ public: ...@@ -162,6 +162,10 @@ public:
TOOLS_DLLPUBLIC friend SvStream& ReadColor( SvStream& rIStream, Color& rColor ); TOOLS_DLLPUBLIC friend SvStream& ReadColor( SvStream& rIStream, Color& rColor );
TOOLS_DLLPUBLIC friend SvStream& WriteColor( SvStream& rOStream, const Color& rColor ); TOOLS_DLLPUBLIC friend SvStream& WriteColor( SvStream& rOStream, const Color& rColor );
// Return color as RGB hex string
// for example "00ff00" for green color
OUString AsRGBHexString();
// get ::basegfx::BColor from this color // get ::basegfx::BColor from this color
::basegfx::BColor getBColor() const { return ::basegfx::BColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0); } ::basegfx::BColor getBColor() const { return ::basegfx::BColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0); }
}; };
......
...@@ -19,6 +19,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \ ...@@ -19,6 +19,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \
tools/qa/cppunit/test_reversemap \ tools/qa/cppunit/test_reversemap \
tools/qa/cppunit/test_stream \ tools/qa/cppunit/test_stream \
tools/qa/cppunit/test_urlobj \ tools/qa/cppunit/test_urlobj \
tools/qa/cppunit/test_color \
)) ))
$(eval $(call gb_CppunitTest_use_api,tools_test, \ $(eval $(call gb_CppunitTest_use_api,tools_test, \
......
/* -*- 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 "cppunit/plugin/TestPlugIn.h"
#include <tools/color.hxx>
namespace
{
class Test: public CppUnit::TestFixture
{
public:
void test_asRGBColor();
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(test_asRGBColor);
CPPUNIT_TEST_SUITE_END();
};
void Test::test_asRGBColor()
{
Color aColor;
aColor = COL_BLACK;
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("000000"));
aColor = COL_WHITE;
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("ffffff"));
aColor = COL_RED;
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("800000"));
aColor = COL_TRANSPARENT;
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("ffffff"));
aColor = COL_BLUE;
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("000080"));
aColor.SetRed(0x12);
aColor.SetGreen(0x34);
aColor.SetBlue(0x56);
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("123456"));
aColor = COL_AUTO;
CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("ffffff"));
}
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -232,6 +232,13 @@ SvStream& Color::Write( SvStream& rOStm, bool bNewFormat ) ...@@ -232,6 +232,13 @@ SvStream& Color::Write( SvStream& rOStm, bool bNewFormat )
return rOStm; return rOStm;
} }
OUString Color::AsRGBHexString()
{
std::stringstream ss;
ss << std::hex << std::setfill ('0') << std::setw(6) << GetRGBColor();
return OUString::createFromAscii(ss.str().c_str());
}
#define COL_NAME_USER ((sal_uInt16)0x8000) #define COL_NAME_USER ((sal_uInt16)0x8000)
SvStream& ReadColor( SvStream& rIStream, Color& rColor ) SvStream& ReadColor( SvStream& rIStream, Color& rColor )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment