Commit 3d9e9ede authored by Guillaume Lazzara's avatar Guillaume Lazzara
Browse files

Add hexadecimal color conversion routines.

	* tests/Makefile.am: Add subdir.

	* scribo/util/color_to_hex.hh,
	* scribo/util/hex_to_color.hh,
	* tests/util/Makefile.am,
	* tests/util/color_to_hex.cc,
	* tests/util/hex_to_color.cc: New.
parent d90f5122
2011-05-05 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr>
Add hexadecimal color conversion routines.
* tests/Makefile.am: Add subdir.
* scribo/util/color_to_hex.hh,
* scribo/util/hex_to_color.hh,
* tests/util/Makefile.am,
* tests/util/color_to_hex.cc,
* tests/util/hex_to_color.cc: New.
2011-05-05 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr>
Explicitly set vertical or horizontal attribute for separators in
......
// Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
#ifndef SCRIBO_UTIL_COLOR_TO_HEX_HH
# define SCRIBO_UTIL_COLOR_TO_HEX_HH
/// \file
///
/// Convert hexadecimal encoded colors to value::rgb8.
#include <cstdio>
#include <iostream>
#include <string.h>
#include <mln/value/rgb8.hh>
namespace scribo
{
namespace util
{
using namespace mln;
std::string color_to_hex(const value::rgb8& v);
# ifndef MLN_INCLUDE_ONLY
std::string color_to_hex(const value::rgb8& v)
{
std::string result = "#";
char buf[3];
int c = v.red();
sprintf(buf, "%.2X", c);
result.append(buf);
c = v.green();
sprintf(buf, "%.2X", c);
result.append(buf);
c = v.blue();
sprintf(buf, "%.2X", c);
result.append(buf);
return result;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace scribo::util
} // end of namespace scribo
#endif // ! SCRIBO_UTIL_COLOR_TO_HEX_HH
// Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
#ifndef SCRIBO_UTIL_HEX_TO_COLOR_HH
# define SCRIBO_UTIL_HEX_TO_COLOR_HH
/// \file
///
/// Convert hexadecimal encoded colors to value::rgb8.
#include <iostream>
#include <string>
#include <vector>
#include <mln/value/rgb8.hh>
#include <mln/value/int_u8.hh>
namespace scribo
{
namespace util
{
using namespace mln;
// \brief Convert hexadecimal encoded colors to value::rgb8.
value::rgb8 hex_to_color(const std::string& hex);
# ifndef MLN_INCLUDE_ONLY
namespace internal
{
value::int_u8 convert_from_hex(const std::string& hex)
{
value::int_u8 value = 0;
int a = 0;
int b = hex.length() - 1;
for (; b >= 0; a++, b--)
{
if (hex[b] >= '0' && hex[b] <= '9')
{
value += (hex[b] - '0') * (1 << (a * 4));
}
else
{
switch (hex[b])
{
case 'A':
case 'a':
value += 10 * (1 << (a * 4));
break;
case 'B':
case 'b':
value += 11 * (1 << (a * 4));
break;
case 'C':
case 'c':
value += 12 * (1 << (a * 4));
break;
case 'D':
case 'd':
value += 13 * (1 << (a * 4));
break;
case 'E':
case 'e':
value += 14 * (1 << (a * 4));
break;
case 'F':
case 'f':
value += 15 * (1 << (a * 4));
break;
default:
std::cerr << "Error, invalid character '"
<< hex[a] << "' in hex number" << std::endl;
break;
}
}
}
return value;
}
} // end of namespace scribo::util::internal
value::rgb8 hex_to_color(const std::string& hex)
{
trace::entering("scribo::util::hex_to_color");
mln_precondition(!hex.empty());
std::string red, green, blue;
if (hex[0] == '#')
{
red = hex.substr(1, 2);
green = hex.substr(3, 2);
blue = hex.substr(5, 2);
}
else
{
red = hex.substr(0, 2);
green = hex.substr(2, 2);
blue = hex.substr(4, 2);
}
value::rgb8 v(internal::convert_from_hex(red),
internal::convert_from_hex(green),
internal::convert_from_hex(blue));
trace::exiting("scribo::util::hex_to_color");
return v;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace scribo::util
} // end of namespace scribo
#endif // ! SCRIBO_UTIL_HEX_TO_COLOR_HH
......@@ -43,7 +43,8 @@ SUBDIRS = \
table \
text \
toolchain \
unit_test
unit_test \
util
# Regen files recursively.
include $(top_srcdir)/build-aux/regen-recursive.mk
......
# Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE).
#
# This file is part of Olena.
#
# Olena is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, version 2 of the License.
#
# Olena 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Olena. If not, see <http://www.gnu.org/licenses/>.
include $(top_srcdir)/scribo/tests/tests.mk
check_PROGRAMS = \
color_to_hex \
hex_to_color
color_to_hex_SOURCES = color_to_hex.cc
hex_to_color_SOURCES = hex_to_color.cc
TESTS = $(check_PROGRAMS)
// Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
// \file
#include <scribo/util/color_to_hex.hh>
int main()
{
using namespace mln;
std::string hex = scribo::util::color_to_hex(value::rgb8(173, 255, 47));
mln_assertion(hex == "#ADFF2F");
}
// Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
// \file
#include <scribo/util/hex_to_color.hh>
int main()
{
using namespace mln;
value::rgb8 v1 = scribo::util::hex_to_color("#ADFF2F");
value::rgb8 v2 = scribo::util::hex_to_color("ADFF2F");
value::rgb8 v3 = scribo::util::hex_to_color("aDfF2F");
mln_assertion(v1 == v2);
mln_assertion(v2 == v3);
mln_assertion(v1 == value::rgb8(173, 255, 47));
}
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