praktika-cpp-2PO07/ColorChooser/Unit1.cpp

144 lines
4.4 KiB
C++
Raw Normal View History

2024-03-02 18:17:55 +02:00
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool up = true;
int mode = 0;
int r=255,g=255,b=255;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{ int h2 = ClientHeight/2;
int w = ClientWidth;
TRect aRect = Rect(0,0,ClientWidth,ClientHeight);
Canvas->Brush->Color=(TColor)0xffffff;
Canvas->FillRect(aRect);
//Blue
TRect b1 = Rect(20,20,120,h2);
Canvas->Brush->Color=(TColor)0;
Canvas->FillRect(b1);
b1 = Rect(20,h2-((b*(h2-20)/255)),120,h2);
Canvas->Brush->Color=clBlue;
Canvas->Pen->Color=clBlue;
Canvas->FillRect(b1);
//Green
TRect g1 = Rect(140,20,240,h2);
Canvas->Brush->Color=(TColor)0;
Canvas->FillRect(g1);
g1 = Rect(140,h2-((g*(h2-20)/255)),240,h2);
Canvas->Brush->Color=(TColor)0x00FF00;
Canvas->Pen->Color=(TColor)0x00FF00;
Canvas->FillRect(g1);
//Red
TRect r1 = Rect(260,20,360,h2);
Canvas->Brush->Color=(TColor)0;
Canvas->FillRect(r1);
r1 = Rect(260,h2-((r*(h2-20)/255)),360,h2);
Canvas->Brush->Color=clRed;
Canvas->Pen->Color=clRed;
Canvas->FillRect(r1);
//Cursor
if(mode==0) b1 = Rect(20,20,120,h2);
else if(mode==1) b1 = Rect(140,20,240,h2);
else if(mode==2) b1 = Rect(260,20,360,h2);
Canvas->Pen->Color=clRed;
Canvas->MoveTo(b1.Left,b1.Bottom);
Canvas->LineTo(b1.right,b1.Bottom);
Canvas->Pen->Color=clGreen;
Canvas->MoveTo(b1.Left,b1.Bottom+1);
Canvas->LineTo(b1.right,b1.Bottom+1);
Canvas->Pen->Color=clBlue;
Canvas->MoveTo(b1.Left,b1.Bottom+2);
Canvas->LineTo(b1.right,b1.Bottom+2);
//BGR
TRect all = Rect(0,h2+10,w,h2*2);
Canvas->Brush->Color=(TColor)(b<<16)|(g<<8)|r;
Canvas->FillRect(all);
//Text
Canvas->Brush->Color=clWhite;
Canvas->Font->Color=clRed;
Canvas->TextOutA(23,ClientHeight-30,"bgr= 0x"+IntToHex((b<<16)|(g<<8)|r,6));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(((int)Key)==VK_LEFT) {
mode--;
if(mode<0) mode=2;
}
if(((int)Key)==VK_RIGHT) {
mode++;
if(mode>2) mode=0;
}
if(((int)Key)==VK_UP) {
if(mode==0) b++;
else if(mode==1) g++;
else if(mode==2) r++;
if(b>255) b=0;
if(g>255) g=0;
if(r>255) r=0;
}
if(((int)Key)==VK_DOWN) {
if(mode==0) b--;
else if(mode==1) g--;
else if(mode==2) r--;
if(b<0) b=255;
if(g<0) g=255;
if(r<0) r=255;
}
FormPaint(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(up) return;
int h2 = ClientHeight/2;
//B
if((X>=20) && (X<=120) && (Y>=20) && (Y<=h2)) {
b = 255-(Y-20)*255/(h2-20);
mode = 0;
}
//G
if((X>=140) && (X<=240) && (Y>=20) && (Y<=h2)) {
g = 255-(Y-20)*255/(h2-20);
mode = 1;
}
//R
if((X>=260) && (X<=360) && (Y>=20) && (Y<=h2)) {
r = 255-(Y-20)*255/(h2-20);
mode = 2;
}
FormPaint(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
up = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
up = true;
}
//---------------------------------------------------------------------------