虹を描いてみよう

C++ Builder CE

C++ Builder CE FMX

いわゆるレインボーカラーを画面に描くだけのプログラムだけどね。まず結果から

こんな感じです。

上記のPeak-meterの記事でサウンド強度を色に変換するルーチンを公開はしていましたが、分かりやすく抜き出しました。

FMXで書いてますけど、肝心な関数は、

  	TAlphaColor t;

tHue,Saturation,Lightnessから計算する、

		t = HSLtoRGB(i/360,0.75,0.5);

が使えるかどうかです。FMXでは使えますが、VCLでは対応する関数がないので作らないといけませんね。作ればVCLでも書けます。フォームにはTlabelx1のみ。Unit1.hは、

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Objects.hpp>
#include <FMX.Types.hpp>
#include <FMX.Colors.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.StdCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE で管理されるコンポーネント
	TPaintBox *PaintBox1;
	TLabel *Label1;
	void __fastcall PaintBox1Paint(TObject *Sender, TCanvas *Canvas);

private:	// ユーザー宣言
public:		// ユーザー宣言
	__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cppは、

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{

	Label1->Text = "Displaying rainbow colors";


}
//---------------------------------------------------------------------------

void __fastcall TForm1::PaintBox1Paint(TObject *Sender, TCanvas *Canvas)
{
   	TAlphaColor t;
	TRectF var,varc;
	float start;
	float apeak = 1.0;

	TCanvas *canvas = PaintBox1->Canvas;

	start = 0;


	for( float i = start ; i <360*apeak ; i += 1.0 ){
		t = HSLtoRGB(i/360,0.75,0.5);
		canvas->Fill->Color = t;
		var = TRectF(i,0,i+1,PaintBox1->Height);
		canvas->FillRect(var,0,0,AllCorners,1.0);
	}


}
//---------------------------------------------------------------------------

理屈としては、音のレベルが低い方から高い方に向けて光の波長にマップするつもりでした。エネルギー的には波長が短い方が高いので違和感はあまりないかもしれませんね。

コメント