Skip to content

Latest commit

 

History

History
40 lines (23 loc) · 4.57 KB

CppRgbGradient.md

File metadata and controls

40 lines (23 loc) · 4.57 KB

 

 

 

 

 

 

For a value from 0.0 to 1.0, return three unsigned chars that when aligned show more or less a rainbow-like gradient (but for the real rainbow gradient see the Rainbow function), like the second row of the picture here. The full CLX code to obtain that picture is shown below the function.

 


#include <cassert> #include <cmath> #include <algorithm> //From http://www.richelbilderbeek.nl/CppRgbGradient.htm void RgbGradient(   const double x,   unsigned char& r,   unsigned char& g,   unsigned char& b) {   r = GetRed(x);   g = GetGreen(x);   b = GetBlue(x); } //From http://www.richelbilderbeek.nl/CppRgbGradient.htm unsigned char GetRed(const double x) {   assert( x >= 0.0 && x < 1.0);   const double f = std::max(0.0,     (x < 0.5     ?  std::cos(x * 1.5 * M_PI)     : -std::sin(x * 1.5 * M_PI)     ) );   assert( f >= 0.0);   assert( f <= 1.0);   const double y = 255.0 * f;   assert( static_cast<int>(y) < 256 );   return static_cast<unsigned char>(y); } //From http://www.richelbilderbeek.nl/CppRgbGradient.htm unsigned char GetGreen(const double x) {   assert( x >= 0.0 && x < 1.0);   const double f = std::max(0.0, std::sin( x * 1.5 * M_PI ) );   assert( f >= 0.0);   assert( f <= 1.0);   const double y = 255.0 * f;   assert( static_cast<int>(y) < 256 );   return static_cast<unsigned char>(y); } //From http://www.richelbilderbeek.nl/CppRgbGradient.htm unsigned char GetBlue(const double x) {   assert( x >= 0.0 && x < 1.0);   const double f = std::max(0.0, -std::cos( x * 1.5 * M_PI ) );   assert( f >= 0.0);   assert( f <= 1.0);   const double y = 255.0 * f;   assert( static_cast<int>(y) < 256 );   return static_cast<unsigned char>(y); }