ColorOperations.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <cmath>
00020 #include <limits>
00021 #include <stdexcept>
00022 
00023 #include <rqt_multiplot/BitOperations.h>
00024 
00025 #include "rqt_multiplot/ColorOperations.h"
00026 
00027 namespace rqt_multiplot {
00028 
00029 /*****************************************************************************/
00030 /* Methods                                                                   */
00031 /*****************************************************************************/
00032 
00033 float ColorOperations::intToHue(unsigned char val) {
00034   return (float)BitOperations::revertByte(val)/
00035     std::numeric_limits<unsigned char>::max();
00036 }
00037 
00038 unsigned char ColorOperations::hueToInt(float hue) {
00039   return BitOperations::revertByte((unsigned int)round(hue/
00040     (2.0*M_PI)*std::numeric_limits<unsigned char>::max()));
00041 }
00042 
00043 QColor ColorOperations::hsvToRgb(const QColor& hsv) {
00044   QColor rgb;
00045   
00046   rgb.setAlphaF(hsv.alphaF());
00047 
00048   if (hsv.blueF() > 0.0) {
00049     float hue = hsv.redF()*2.0*M_PI/(60.0*M_PI/180.0);
00050     int i = floor(hue);
00051     float f = hue-i;
00052     float p = hsv.blueF()*(1.0-hsv.greenF());
00053     float q = hsv.blueF()*(1.0-hsv.greenF()*f);
00054     float t = hsv.blueF()*(1.0-hsv.greenF()*(1.0-f));
00055 
00056     switch (i) {
00057       case 0:
00058         rgb.setRedF(hsv.blueF());
00059         rgb.setGreenF(t);
00060         rgb.setBlueF(p);
00061         break;
00062       case 1:
00063         rgb.setRedF(q);
00064         rgb.setGreenF(hsv.blueF());
00065         rgb.setBlueF(p);
00066         break;
00067       case 2:
00068         rgb.setRedF(p);
00069         rgb.setGreenF(hsv.blueF());
00070         rgb.setBlueF(t);
00071         break;
00072       case 3:
00073         rgb.setRedF(p);
00074         rgb.setGreenF(q);
00075         rgb.setBlueF(hsv.blueF());
00076         break;
00077       case 4:
00078         rgb.setRedF(t);
00079         rgb.setGreenF(p);
00080         rgb.setBlueF(hsv.blueF());
00081         break;
00082       default:
00083         rgb.setRedF(hsv.blueF());
00084         rgb.setGreenF(p);
00085         rgb.setBlueF(q);
00086         break;
00087     }
00088   }
00089   else {
00090     rgb.setRedF(hsv.blueF());
00091     rgb.setGreenF(hsv.blueF());
00092     rgb.setBlueF(hsv.blueF());
00093   }
00094 
00095   return rgb;
00096 }
00097 
00098 QColor ColorOperations::intToRgb(unsigned char val) {
00099   QColor hsv;
00100   hsv.setRgbF(intToHue(val), 1.0, 1.0, 1.0);
00101 
00102   return hsvToRgb(hsv);
00103 }
00104 
00105 QColor ColorOperations::invertRgb(const QColor& rgb) {
00106   return QColor::fromRgbF(1.0-rgb.redF(), 1.0-rgb.greenF(), 1.0-rgb.blueF(),
00107     rgb.alphaF());
00108 }
00109 
00110 };


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Thu Jun 6 2019 21:49:10