
* 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. --- scribo/ChangeLog | 12 ++ .../serializable.hh => util/color_to_hex.hh} | 54 +++++--- scribo/scribo/util/hex_to_color.hh | 145 ++++++++++++++++++++ scribo/tests/Makefile.am | 3 +- scribo/tests/{convert => util}/Makefile.am | 11 +- .../util/color_to_hex.cc} | 18 +-- .../ocr_options.hh => tests/util/hex_to_color.cc} | 30 ++--- 7 files changed, 217 insertions(+), 56 deletions(-) copy scribo/scribo/{core/concept/serializable.hh => util/color_to_hex.hh} (64%) create mode 100644 scribo/scribo/util/hex_to_color.hh copy scribo/tests/{convert => util}/Makefile.am (85%) copy scribo/{demo/viewer/option_widget.cc => tests/util/color_to_hex.cc} (70%) copy scribo/{demo/viewer/ocr_options.hh => tests/util/hex_to_color.cc} (60%) diff --git a/scribo/ChangeLog b/scribo/ChangeLog index e857752..e30b230 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,5 +1,17 @@ 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 XML output. diff --git a/scribo/scribo/core/concept/serializable.hh b/scribo/scribo/util/color_to_hex.hh similarity index 64% copy from scribo/scribo/core/concept/serializable.hh copy to scribo/scribo/util/color_to_hex.hh index 6e661a6..e0ee33f 100644 --- a/scribo/scribo/core/concept/serializable.hh +++ b/scribo/scribo/util/color_to_hex.hh @@ -23,42 +23,56 @@ // exception does not however invalidate any other reasons why the // executable file might be covered by the GNU General Public License. -#ifndef SCRIBO_CORE_CONCEPT_SERIALIZABLE_HH -# define SCRIBO_CORE_CONCEPT_SERIALIZABLE_HH +#ifndef SCRIBO_UTIL_COLOR_TO_HEX_HH +# define SCRIBO_UTIL_COLOR_TO_HEX_HH /// \file /// -/// Concept for serializer visitors. +/// Convert hexadecimal encoded colors to value::rgb8. -# include <mln/core/concept/object.hh> -# include <scribo/core/concept/serialize_visitor.hh> + +#include <cstdio> +#include <iostream> +#include <string.h> +#include <mln/value/rgb8.hh> namespace scribo { - /// \brief Link functor concept. - template <typename E> - class Serializable : public mln::Object<E> + namespace util { - public: - template <typename E2> - void accept(const SerializeVisitor<E2>& visitor) const; - }; + using namespace mln; + + std::string color_to_hex(const value::rgb8& v); # ifndef MLN_INCLUDE_ONLY - template <typename E> - template <typename E2> - void - Serializable<E>::accept(const SerializeVisitor<E2>& visitor) const - { - exact(visitor).visit(exact(*this)); - } + 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_CORE_CONCEPT_SERIALIZABLE_HH +#endif // ! SCRIBO_UTIL_COLOR_TO_HEX_HH diff --git a/scribo/scribo/util/hex_to_color.hh b/scribo/scribo/util/hex_to_color.hh new file mode 100644 index 0000000..d1368e2 --- /dev/null +++ b/scribo/scribo/util/hex_to_color.hh @@ -0,0 +1,145 @@ +// 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 diff --git a/scribo/tests/Makefile.am b/scribo/tests/Makefile.am index 706338f..e2e7e66 100644 --- a/scribo/tests/Makefile.am +++ b/scribo/tests/Makefile.am @@ -43,7 +43,8 @@ SUBDIRS = \ table \ text \ toolchain \ - unit_test + unit_test \ + util # Regen files recursively. include $(top_srcdir)/build-aux/regen-recursive.mk diff --git a/scribo/tests/convert/Makefile.am b/scribo/tests/util/Makefile.am similarity index 85% copy from scribo/tests/convert/Makefile.am copy to scribo/tests/util/Makefile.am index c4c912c..8d132aa 100644 --- a/scribo/tests/convert/Makefile.am +++ b/scribo/tests/util/Makefile.am @@ -13,15 +13,14 @@ # # You should have received a copy of the GNU General Public License # along with Olena. If not, see <http://www.gnu.org/licenses/>. -# - -## Process this file through Automake to create Makefile.in. include $(top_srcdir)/scribo/tests/tests.mk -check_PROGRAMS = \ - base64 +check_PROGRAMS = \ + color_to_hex \ + hex_to_color -base64_SOURCES = base64.cc +color_to_hex_SOURCES = color_to_hex.cc +hex_to_color_SOURCES = hex_to_color.cc TESTS = $(check_PROGRAMS) diff --git a/scribo/demo/viewer/option_widget.cc b/scribo/tests/util/color_to_hex.cc similarity index 70% copy from scribo/demo/viewer/option_widget.cc copy to scribo/tests/util/color_to_hex.cc index 4557749..8e76b07 100644 --- a/scribo/demo/viewer/option_widget.cc +++ b/scribo/tests/util/color_to_hex.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -14,17 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Olena. If not, see <http://www.gnu.org/licenses/>. -# include "option_widget.hh" +// \file -OptionWidget::OptionWidget(QWidget * parent) - : QWidget(parent) -{ -} +#include <scribo/util/color_to_hex.hh> -void OptionWidget::save_config() +int main() { -} + using namespace mln; -void OptionWidget::load_config() -{ + std::string hex = scribo::util::color_to_hex(value::rgb8(173, 255, 47)); + + mln_assertion(hex == "#ADFF2F"); } diff --git a/scribo/demo/viewer/ocr_options.hh b/scribo/tests/util/hex_to_color.cc similarity index 60% copy from scribo/demo/viewer/ocr_options.hh copy to scribo/tests/util/hex_to_color.cc index 67ab4ef..65ea79e 100644 --- a/scribo/demo/viewer/ocr_options.hh +++ b/scribo/tests/util/hex_to_color.cc @@ -14,27 +14,19 @@ // You should have received a copy of the GNU General Public License // along with Olena. If not, see <http://www.gnu.org/licenses/>. -#ifndef SCRIBO_DEMO_VIEWER_OCR_OPTIONS_HH -# define SCRIBO_DEMO_VIEWER_OCR_OPTIONS_HH +// \file -# include <QtGui> -# include <ocr_options.ui.h> -# include "option_widget.hh" +#include <scribo/util/hex_to_color.hh> -class ocr_options : public OptionWidget, private Ui::OcrOptions +int main() { - Q_OBJECT; + using namespace mln; -public: - ocr_options(QWidget *parent = 0); - ~ocr_options(); + 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"); - void load_config(); - void save_config(); - -private: - int find_index(const QString& lang); - -}; - -#endif // ! SCRIBO_DEMO_VIEWER_OCR_OPTIONS_HH + mln_assertion(v1 == v2); + mln_assertion(v2 == v3); + mln_assertion(v1 == value::rgb8(173, 255, 47)); +} -- 1.5.6.5