From: Thomas Geymayer Date: Mon, 5 Nov 2012 16:58:24 +0000 (+0100) Subject: Move parseColor to scene/util X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=53dea9b069f2d72902aa8f3be7838f83cc7e5726;p=simgear.git Move parseColor to scene/util parseColor is used to parse a CSS color string into an osg::Vec4 which is only available in SimGearScene. If someone needs the function also in SimGear headless mode we have to think about where to better place this function. --- diff --git a/simgear/canvas/Canvas.cxx b/simgear/canvas/Canvas.cxx index acb8da88..c998f710 100644 --- a/simgear/canvas/Canvas.cxx +++ b/simgear/canvas/Canvas.cxx @@ -18,7 +18,7 @@ #include "Canvas.hxx" #include -#include +#include #include #include diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 811c6c17..2d07d8e8 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include diff --git a/simgear/canvas/elements/CanvasPath.cxx b/simgear/canvas/elements/CanvasPath.cxx index ac9d6a9c..bdbd17c6 100644 --- a/simgear/canvas/elements/CanvasPath.cxx +++ b/simgear/canvas/elements/CanvasPath.cxx @@ -17,7 +17,7 @@ // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA #include "CanvasPath.hxx" -#include +#include #include #include diff --git a/simgear/canvas/elements/CanvasText.cxx b/simgear/canvas/elements/CanvasText.cxx index fdaa7895..2ddf39d4 100644 --- a/simgear/canvas/elements/CanvasText.cxx +++ b/simgear/canvas/elements/CanvasText.cxx @@ -19,7 +19,7 @@ #include "CanvasText.hxx" #include #include -#include +#include #include namespace simgear diff --git a/simgear/misc/CMakeLists.txt b/simgear/misc/CMakeLists.txt index 8ef0f2e3..d07b6718 100644 --- a/simgear/misc/CMakeLists.txt +++ b/simgear/misc/CMakeLists.txt @@ -4,7 +4,6 @@ include (SimGearComponent) set(HEADERS ResourceManager.hxx interpolator.hxx - parse_color.hxx sg_dir.hxx sg_path.hxx sgstream.hxx @@ -20,7 +19,6 @@ set(HEADERS set(SOURCES ResourceManager.cxx interpolator.cxx - parse_color.cxx sg_dir.cxx sg_path.cxx sgstream.cxx @@ -41,10 +39,6 @@ add_executable(test_strings strutils_test.cxx ) add_test(test_strings ${EXECUTABLE_OUTPUT_PATH}/test_strings) target_link_libraries(test_strings SimGearCore) -add_executable(test_parse_color parse_color_test.cxx ) -add_test(test_parse_color ${EXECUTABLE_OUTPUT_PATH}/test_parse_color) -target_link_libraries(test_parse_color SimGearCore) - add_executable(test_path path_test.cxx ) add_test(test_path ${EXECUTABLE_OUTPUT_PATH}/test_path) target_link_libraries(test_path SimGearCore) diff --git a/simgear/misc/parse_color.cxx b/simgear/misc/parse_color.cxx deleted file mode 100644 index b7395224..00000000 --- a/simgear/misc/parse_color.cxx +++ /dev/null @@ -1,106 +0,0 @@ -// Parse CSS colors -// -// Copyright (C) 2012 Thomas Geymayer -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - -#include -#ifndef SIMGEAR_HEADLESS -# include -#endif -#include "parse_color.hxx" - -#include -#include -#include -#include - -namespace simgear -{ - - //---------------------------------------------------------------------------- - bool parseColor(std::string str, SGVec4f& result) - { - boost::trim(str); - SGVec4f color(0,0,0,1); - - if( str.empty() ) - return false; - - // #rrggbb - if( str[0] == '#' ) - { - const int offsets[] = {2,2,2}; - const boost::offset_separator hex_separator( boost::begin(offsets), - boost::end(offsets) ); - typedef boost::tokenizer offset_tokenizer; - offset_tokenizer tokens(str.begin() + 1, str.end(), hex_separator); - - int comp = 0; - for( offset_tokenizer::const_iterator tok = tokens.begin(); - tok != tokens.end() && comp < 4; - ++tok, ++comp ) - { - color[comp] = strtol(std::string(*tok).c_str(), 0, 16) / 255.f; - } - } - // rgb(r,g,b) - // rgba(r,g,b,a) - else if( boost::ends_with(str, ")") ) - { - const std::string RGB = "rgb(", - RGBA = "rgba("; - size_t pos; - if( boost::starts_with(str, RGB) ) - pos = RGB.length(); - else if( boost::starts_with(str, RGBA) ) - pos = RGBA.length(); - else - return false; - - typedef boost::tokenizer > tokenizer; - const boost::char_separator del(", \t\n"); - - tokenizer tokens(str.begin() + pos, str.end() - 1, del); - int comp = 0; - for( tokenizer::const_iterator tok = tokens.begin(); - tok != tokens.end() && comp < 4; - ++tok, ++comp ) - { - color[comp] = boost::lexical_cast(*tok) - // rgb = [0,255], a = [0,1] - / (comp < 3 ? 255 : 1); - } - } - else - return false; - - result = color; - return true; - } - -#ifndef SIMGEAR_HEADLESS - bool parseColor(std::string str, osg::Vec4& result) - { - SGVec4f color; - if( !parseColor(str, color) ) - return false; - - result.set(color[0], color[1], color[2], color[3]); - return true; - } -#endif - -} // namespace simgear diff --git a/simgear/misc/parse_color.hxx b/simgear/misc/parse_color.hxx deleted file mode 100644 index 83d662b7..00000000 --- a/simgear/misc/parse_color.hxx +++ /dev/null @@ -1,55 +0,0 @@ -// Parse CSS colors -// -// Copyright (C) 2012 Thomas Geymayer -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - -#ifndef PARSE_COLOR_HXX_ -#define PARSE_COLOR_HXX_ - -#include -#include -#include - -#include - -namespace simgear -{ - - /** - * Parse a (CSS) color - * - * @param str Text to parse - * @param result Output for parse color - * - * @return Whether str contained a valid color (and result has been modified) - */ - bool parseColor(std::string str, SGVec4f& result); - -#ifdef OSG_VEC4 - /** - * Parse a (CSS) color into an osg::Vec4 - * - * @param str Text to parse - * @param result Output for parse color - * - * @return Whether str contained a valid color (and result has been modified) - */ - bool parseColor(std::string str, osg::Vec4& result); -#endif - -} // namespace simgear - -#endif /* PARSE_COLOR_HXX_ */ diff --git a/simgear/misc/parse_color_test.cxx b/simgear/misc/parse_color_test.cxx deleted file mode 100644 index c3795e65..00000000 --- a/simgear/misc/parse_color_test.cxx +++ /dev/null @@ -1,35 +0,0 @@ -#include - -#include "parse_color.hxx" - -#include - -#define COMPARE(a, b) \ - if( (a) != (b) ) \ - { \ - std::cerr << "failed:" << #a << " != " << #b << std::endl; \ - return 1; \ - } - -#define VERIFY(a) \ - if( !(a) ) \ - { \ - std::cerr << "failed:" << #a << std::endl; \ - return 1; \ - } - -#define VERIFY_COLOR(str, r, g, b, a) \ - VERIFY(simgear::parseColor(str, color)) \ - COMPARE(color, SGVec4f(r, g, b, a)) - -int main (int ac, char ** av) -{ - SGVec4f color; - VERIFY_COLOR("#ff0000", 1,0,0,1); - VERIFY_COLOR("#00ff00", 0,1,0,1); - VERIFY_COLOR("#0000ff", 0,0,1,1); - VERIFY_COLOR("rgb( 255,\t127.5,0)", 1, 0.5, 0, 1); - VERIFY_COLOR("rgba(255, 127.5,0, 0.5)", 1, 0.5, 0, 0.5); - std::cout << "all tests passed successfully!" << std::endl; - return 0; -} diff --git a/simgear/scene/util/CMakeLists.txt b/simgear/scene/util/CMakeLists.txt index 124f0ba5..d10a4204 100644 --- a/simgear/scene/util/CMakeLists.txt +++ b/simgear/scene/util/CMakeLists.txt @@ -8,6 +8,7 @@ set(HEADERS OptionsReadFileCallback.hxx OsgMath.hxx OsgSingleton.hxx + parse_color.hxx PrimitiveUtils.hxx QuadTreeBuilder.hxx RenderConstants.hxx @@ -34,6 +35,7 @@ set(SOURCES NodeAndDrawableVisitor.cxx Noise.cxx OptionsReadFileCallback.cxx + parse_color.cxx PrimitiveUtils.cxx QuadTreeBuilder.cxx SGEnlargeBoundingBox.cxx @@ -49,3 +51,9 @@ set(SOURCES ) simgear_scene_component(util scene/util "${SOURCES}" "${HEADERS}") + +if(ENABLE_TESTS) +add_executable(test_parse_color parse_color_test.cxx ) +add_test(test_parse_color ${EXECUTABLE_OUTPUT_PATH}/test_parse_color) +target_link_libraries(test_parse_color SimGearScene) +endif(ENABLE_TESTS) diff --git a/simgear/scene/util/parse_color.cxx b/simgear/scene/util/parse_color.cxx new file mode 100644 index 00000000..f5947122 --- /dev/null +++ b/simgear/scene/util/parse_color.cxx @@ -0,0 +1,90 @@ +// Parse CSS colors +// +// Copyright (C) 2012 Thomas Geymayer +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + +#include "parse_color.hxx" + +#include +#include +#include +#include + +namespace simgear +{ + + //---------------------------------------------------------------------------- + bool parseColor(std::string str, osg::Vec4& result) + { + boost::trim(str); + osg::Vec4 color(0,0,0,1); + + if( str.empty() ) + return false; + + // #rrggbb + if( str[0] == '#' ) + { + const int offsets[] = {2,2,2}; + const boost::offset_separator hex_separator( boost::begin(offsets), + boost::end(offsets) ); + typedef boost::tokenizer offset_tokenizer; + offset_tokenizer tokens(str.begin() + 1, str.end(), hex_separator); + + int comp = 0; + for( offset_tokenizer::const_iterator tok = tokens.begin(); + tok != tokens.end() && comp < 4; + ++tok, ++comp ) + { + color[comp] = strtol(std::string(*tok).c_str(), 0, 16) / 255.f; + } + } + // rgb(r,g,b) + // rgba(r,g,b,a) + else if( boost::ends_with(str, ")") ) + { + const std::string RGB = "rgb(", + RGBA = "rgba("; + size_t pos; + if( boost::starts_with(str, RGB) ) + pos = RGB.length(); + else if( boost::starts_with(str, RGBA) ) + pos = RGBA.length(); + else + return false; + + typedef boost::tokenizer > tokenizer; + const boost::char_separator del(", \t\n"); + + tokenizer tokens(str.begin() + pos, str.end() - 1, del); + int comp = 0; + for( tokenizer::const_iterator tok = tokens.begin(); + tok != tokens.end() && comp < 4; + ++tok, ++comp ) + { + color[comp] = boost::lexical_cast(*tok) + // rgb = [0,255], a = [0,1] + / (comp < 3 ? 255 : 1); + } + } + else + return false; + + result = color; + return true; + } + +} // namespace simgear diff --git a/simgear/scene/util/parse_color.hxx b/simgear/scene/util/parse_color.hxx new file mode 100644 index 00000000..d4ecad3c --- /dev/null +++ b/simgear/scene/util/parse_color.hxx @@ -0,0 +1,40 @@ +// Parse CSS colors +// +// Copyright (C) 2012 Thomas Geymayer +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + +#ifndef PARSE_COLOR_HXX_ +#define PARSE_COLOR_HXX_ + +#include +#include + +namespace simgear +{ + + /** + * Parse a (CSS) color + * + * @param str Text to parse + * @param result Output for parse color + * + * @return Whether str contained a valid color (and result has been modified) + */ + bool parseColor(std::string str, osg::Vec4& result); + +} // namespace simgear + +#endif /* PARSE_COLOR_HXX_ */ diff --git a/simgear/scene/util/parse_color_test.cxx b/simgear/scene/util/parse_color_test.cxx new file mode 100644 index 00000000..c41b2645 --- /dev/null +++ b/simgear/scene/util/parse_color_test.cxx @@ -0,0 +1,35 @@ +#include + +#include "parse_color.hxx" + +#include + +#define COMPARE(a, b) \ + if( (a) != (b) ) \ + { \ + std::cerr << "failed:" << #a << " != " << #b << std::endl; \ + return 1; \ + } + +#define VERIFY(a) \ + if( !(a) ) \ + { \ + std::cerr << "failed:" << #a << std::endl; \ + return 1; \ + } + +#define VERIFY_COLOR(str, r, g, b, a) \ + VERIFY(simgear::parseColor(str, color)) \ + COMPARE(color, osg::Vec4(r, g, b, a)) + +int main (int ac, char ** av) +{ + osg::Vec4 color; + VERIFY_COLOR("#ff0000", 1,0,0,1); + VERIFY_COLOR("#00ff00", 0,1,0,1); + VERIFY_COLOR("#0000ff", 0,0,1,1); + VERIFY_COLOR("rgb( 255,\t127.5,0)", 1, 0.5, 0, 1); + VERIFY_COLOR("rgba(255, 127.5,0, 0.5)", 1, 0.5, 0, 0.5); + std::cout << "all tests passed successfully!" << std::endl; + return 0; +}