parse_color.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
31 
32 namespace rviz
33 {
34 static int limit(int i)
35 {
36  if (i < 0)
37  return 0;
38  if (i > 255)
39  return 255;
40  return i;
41 }
42 
43 QColor parseColor(const QString& color_string)
44 {
45  if (color_string.indexOf(';') != -1)
46  {
47  QStringList strings = color_string.split(';');
48  if (strings.size() >= 3)
49  {
50  bool r_ok = true;
51  int r = strings[0].toInt(&r_ok);
52  bool g_ok = true;
53  int g = strings[1].toInt(&g_ok);
54  bool b_ok = true;
55  int b = strings[2].toInt(&b_ok);
56  if (r_ok && g_ok && b_ok)
57  {
58  return QColor(limit(r), limit(g), limit(b));
59  }
60  }
61  return QColor();
62  }
63 
64  QColor new_color;
65  if (QColor::colorNames().contains(color_string, Qt::CaseInsensitive) ||
66  (color_string.size() > 0 && color_string[0] == '#'))
67  {
68  new_color.setNamedColor(color_string.toLower());
69  }
70  return new_color;
71 }
72 
73 QString printColor(const QColor& color)
74 {
75  return QString("%1; %2; %3").arg(color.red()).arg(color.green()).arg(color.blue());
76 }
77 
78 QColor ogreToQt(const Ogre::ColourValue& c)
79 {
80  return QColor::fromRgbF(c.r, c.g, c.b, c.a);
81 }
82 
83 Ogre::ColourValue qtToOgre(const QColor& c)
84 {
85  return Ogre::ColourValue(c.redF(), c.greenF(), c.blueF(), c.alphaF());
86 }
87 
88 } // end namespace rviz
static int limit(int i)
Definition: parse_color.cpp:34
Ogre::ColourValue qtToOgre(const QColor &c)
Definition: parse_color.cpp:83
QColor ogreToQt(const Ogre::ColourValue &c)
Definition: parse_color.cpp:78
QColor parseColor(const QString &color_string)
Definition: parse_color.cpp:43
QString printColor(const QColor &color)
Definition: parse_color.cpp:73
r


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Aug 15 2022 02:05:17