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.
This commit is contained in:
Thomas Geymayer
2012-11-05 17:58:24 +01:00
parent 186bae2bd4
commit 53dea9b069
9 changed files with 17 additions and 46 deletions
-6
View File
@@ -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)
-106
View File
@@ -1,106 +0,0 @@
// Parse CSS colors
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// 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 <simgear_config.h>
#ifndef SIMGEAR_HEADLESS
# include <osg/Vec4>
#endif
#include "parse_color.hxx"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
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<boost::offset_separator> 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<boost::char_separator<char> > tokenizer;
const boost::char_separator<char> 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<float>(*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
-55
View File
@@ -1,55 +0,0 @@
// Parse CSS colors
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// 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 <simgear/math/SGLimits.hxx>
#include <simgear/math/SGMathFwd.hxx>
#include <simgear/math/SGVec4.hxx>
#include <string>
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_ */
-35
View File
@@ -1,35 +0,0 @@
#include <simgear/compiler.h>
#include "parse_color.hxx"
#include <iostream>
#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;
}