Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Rainbow

 

Rainbow is a graphics class to create a rainbow.

 

 

 

 

 

 

rainbow.h

 

//---------------------------------------------------------------------------
/*
Rainbow, class for a rainbow color gradient
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppRainbow.htm
//---------------------------------------------------------------------------
#ifndef RAINBOW_H
#define RAINBOW_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
struct Rainbow
{
  ///Obtain the RGB values from a relative position
  static void GetRgb(
    const double x,
    double& r,
    double& g,
    double& b);

  ///Obtain this class its version
  static const std::string GetVersion();

  ///Obtain this class its version history
  static const std::vector<std::string> GetVersionHistory();
};
//---------------------------------------------------------------------------
#endif // RAINBOW_H

 

 

 

 

 

rainbow.cpp

 

//---------------------------------------------------------------------------
/*
Rainbow, class for a rainbow color gradient
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppRainbow.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
//---------------------------------------------------------------------------
#include "rainbow.h"
//---------------------------------------------------------------------------
void Rainbow::GetRgb(
  const double x,
  double& r,
  double& g,
  double& b)
{
  assert(x >= 0.0);
  assert(x <= 1.0);

  const double f_r = std::max(0.0,
    (x < 0.5
    ? std::cos(x * 1.5 * M_PI)
    : -std::sin(x * 1.5 * M_PI)
    ) );
  const double f_g = std::max(0.0, std::sin( x * 1.5 * M_PI ) );
  const double f_b = std::max(0.0, -std::cos( x * 1.5 * M_PI ) );
  const double max = std::max(f_r, std::max(f_g,f_b));
  assert(max!=0);

  r = f_r / max;
  g = f_g / max;
  b = f_b / max;
}
//---------------------------------------------------------------------------
const std::string Rainbow::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> Rainbow::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-08-31: Version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml