ColorOperations.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <cmath>
20 #include <limits>
21 #include <stdexcept>
22 
24 
26 
27 namespace rqt_multiplot {
28 
29 /*****************************************************************************/
30 /* Methods */
31 /*****************************************************************************/
32 
33 float ColorOperations::intToHue(unsigned char val) {
34  return (float)BitOperations::revertByte(val)/
35  std::numeric_limits<unsigned char>::max();
36 }
37 
38 unsigned char ColorOperations::hueToInt(float hue) {
39  return BitOperations::revertByte((unsigned int)round(hue/
40  (2.0*M_PI)*std::numeric_limits<unsigned char>::max()));
41 }
42 
43 QColor ColorOperations::hsvToRgb(const QColor& hsv) {
44  QColor rgb;
45 
46  rgb.setAlphaF(hsv.alphaF());
47 
48  if (hsv.blueF() > 0.0) {
49  float hue = hsv.redF()*2.0*M_PI/(60.0*M_PI/180.0);
50  int i = floor(hue);
51  float f = hue-i;
52  float p = hsv.blueF()*(1.0-hsv.greenF());
53  float q = hsv.blueF()*(1.0-hsv.greenF()*f);
54  float t = hsv.blueF()*(1.0-hsv.greenF()*(1.0-f));
55 
56  switch (i) {
57  case 0:
58  rgb.setRedF(hsv.blueF());
59  rgb.setGreenF(t);
60  rgb.setBlueF(p);
61  break;
62  case 1:
63  rgb.setRedF(q);
64  rgb.setGreenF(hsv.blueF());
65  rgb.setBlueF(p);
66  break;
67  case 2:
68  rgb.setRedF(p);
69  rgb.setGreenF(hsv.blueF());
70  rgb.setBlueF(t);
71  break;
72  case 3:
73  rgb.setRedF(p);
74  rgb.setGreenF(q);
75  rgb.setBlueF(hsv.blueF());
76  break;
77  case 4:
78  rgb.setRedF(t);
79  rgb.setGreenF(p);
80  rgb.setBlueF(hsv.blueF());
81  break;
82  default:
83  rgb.setRedF(hsv.blueF());
84  rgb.setGreenF(p);
85  rgb.setBlueF(q);
86  break;
87  }
88  }
89  else {
90  rgb.setRedF(hsv.blueF());
91  rgb.setGreenF(hsv.blueF());
92  rgb.setBlueF(hsv.blueF());
93  }
94 
95  return rgb;
96 }
97 
98 QColor ColorOperations::intToRgb(unsigned char val) {
99  QColor hsv;
100  hsv.setRgbF(intToHue(val), 1.0, 1.0, 1.0);
101 
102  return hsvToRgb(hsv);
103 }
104 
105 QColor ColorOperations::invertRgb(const QColor& rgb) {
106  return QColor::fromRgbF(1.0-rgb.redF(), 1.0-rgb.greenF(), 1.0-rgb.blueF(),
107  rgb.alphaF());
108 }
109 
110 };
static QColor invertRgb(const QColor &rgb)
f
static QColor hsvToRgb(const QColor &hsv)
static QColor intToRgb(unsigned char val)
static float intToHue(unsigned char val)
static unsigned char revertByte(unsigned char val)
static unsigned char hueToInt(float hue)


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44