Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

common-light.h

Go to the documentation of this file.
00001 /*!@addtogroup common_includes
00002  * @{
00003  * @defgroup light-common_h Light Functions
00004  * Functions common to light sensor drivers
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: common-light.h 108 2012-09-16 09:06:13Z xander $
00010  */
00011 
00012 #ifndef __LIGHT_COMMON_H__
00013 #define __LIGHT_COMMON_H__
00014 
00015 #ifndef __COMMON_H__
00016 #include "common.h"
00017 #endif
00018 
00019 /** \file common-light.h
00020  * \brief Functions common to light sensor drivers
00021  *
00022  * common-light.h holds functions common to light sensor drivers.
00023  * Contains code to convert from RGB colors to HSV colors.
00024  *
00025  * \author Mike Henning, Max Bareiss
00026  * \author Xander Soldaat (minor modifications)
00027  * \date 27 April 2011
00028  */
00029 
00030 #pragma systemFile
00031 
00032 
00033 /**
00034  * Convert RGB colors to HSV
00035  * @param red the red input value
00036  * @param green the green input value
00037  * @param blue the blue input value
00038  * @param hue the hue output value (from 0 to 365, or -1 if n/a)
00039  * @param sat the saruration output value (from 0 to 100, or -1 if n/a)
00040  * @param value the value output value (from 0 to 100)
00041  * @return void
00042  */
00043 void RGBtoHSV(float red, float green, float blue, float &hue, float &sat, float &value)
00044 {
00045         hue = 0;
00046         sat = 0;
00047         value = 0;
00048 
00049   //   Value
00050   float rgb_max = max3(red, green, blue);
00051   float rgb_min;
00052   value = rgb_max / 2.56;
00053   if (value == 0){
00054     hue = -1;
00055     sat = -1;
00056     return;
00057   }
00058 
00059   //   Saturation
00060   red /= rgb_max;
00061   green /= rgb_max;
00062   blue /= rgb_max;
00063 
00064   rgb_max = max3(red, green, blue);
00065   rgb_min = min3(red, green, blue);
00066   sat = (rgb_max - rgb_min) * 100;
00067   if (sat == 0){
00068     hue = -1;
00069     return;
00070   }
00071 
00072   //   Hue
00073 
00074   red = (red - rgb_min) / (rgb_max - rgb_min);
00075   green = (green - rgb_min) / (rgb_max - rgb_min);
00076   blue = (blue - rgb_min) / (rgb_max - rgb_min);
00077 
00078   rgb_max = max3(red, green,blue);
00079   rgb_min = min3(red, green,blue);
00080 
00081   if (rgb_max == red){
00082     hue = 0.0 + 60.0*(green-blue);
00083     if (hue < 0.0){
00084       hue += 360.0;
00085     }
00086   } else if (rgb_max == green){
00087     hue = 120.0 + 60.0 * (blue-red);
00088   } else {
00089     hue = 240.0 + 60.0 * (red-green);
00090   }
00091 }
00092 
00093 #endif __LIGHT_COMMON_H__
00094 
00095 /*
00096  * $Id: common-light.h 108 2012-09-16 09:06:13Z xander $
00097  */
00098 /* @} */
00099 /* @} */