Nadesłany przez Tomasz Lubiński, 05 listopada 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
RGB - C++/Unit1.cpp:
// Model RGB // www.algorytm.org // (c)2007 by Tomasz Lubinski //--------------------------------------------------------------------------- #include <vcl.h> #include "math.h" #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } void __fastcall TForm1::Button1Click(TObject *Sender) { int r,g,b,step; int width, shift_x, shift_y; int side, side_sign; Model->Canvas->Brush->Color = clWhite; Model->Canvas->Rectangle(0, 0, Model->Width, Model->Height); width = StrToInt(Width->Text); shift_x = Model->Width / 2 - width / 1.5; shift_y = width * 1.5 + 50; if (black->Checked) { side = 0; side_sign = -1; } else { side = 255; side_sign = 1; } for (int i=0; i<width; i++) { for (int j=0; j<width; j++) { g = side; b = side - (255.0/width) * i * side_sign; r = side - (255.0/width) * j * side_sign; if (i==0 || j==0 || j == width -1) { g = 0; b= 0; r = 0; } Model->Canvas->Pixels[i+shift_x][shift_y-j] = (TColor)r + (g << 8) + (b << 16);; } } for (int i=0; i<width; i++) { for (int j=0; j<width/2; j++) { r = 255 - side; b = side - (255.0/width) * i * side_sign; g = side - (255.0/(width/2)) * j * side_sign; if (i==0 || i == width-1 || j == width/2 -1) { b= 0; g = 0; r = 0; } Model->Canvas->Pixels[i+j+shift_x][shift_y-j-width] = (TColor)r + (g << 8) + (b << 16);; } } for (int i=0; i<width/2; i++) { for (int j=0; j<width; j++) { b = 255 - side; g = side - (255.0/(width/2)) * i * side_sign; r = side - (255.0/width) * j * side_sign; if (i==0 || j==0 || i == width/2-1) { b= 0; g = 0; r = 0; } Model->Canvas->Pixels[i+shift_x+width][shift_y-j-i-1] = (TColor)r + (g << 8) + (b << 16);; } } Model->Canvas->MoveTo(shift_x, shift_y + 20); Model->Canvas->LineTo(shift_x + width, shift_y + 20); Model->Canvas->LineTo(shift_x + width - 5, shift_y + 15); Model->Canvas->MoveTo(shift_x + width, shift_y + 20); Model->Canvas->LineTo(shift_x + width - 5, shift_y + 25); Model->Canvas->TextOutA(shift_x + 10, shift_y + 7, "B"); Model->Canvas->MoveTo(shift_x - 20, shift_y); Model->Canvas->LineTo(shift_x - 20, shift_y - width); Model->Canvas->LineTo(shift_x - 25, shift_y - width + 5); Model->Canvas->MoveTo(shift_x - 20, shift_y - width); Model->Canvas->LineTo(shift_x - 15, shift_y - width + 5); Model->Canvas->TextOutA(shift_x - 15, shift_y - 20, "R"); Model->Canvas->MoveTo(shift_x + width + 2, shift_y + 18); Model->Canvas->LineTo(shift_x + width * 1.5 + 2, shift_y + 18 - width / 2); Model->Canvas->LineTo(shift_x + width * 1.5 - 5, shift_y + 18 - width / 2); Model->Canvas->MoveTo(shift_x + width * 1.5 + 2, shift_y + 18 - width / 2); Model->Canvas->LineTo(shift_x + width * 1.5 + 2, shift_y + 25 - width / 2); Model->Canvas->TextOutA(shift_x + width + 10, shift_y - 10, "G"); } //--------------------------------------------------------------------------- void __fastcall TForm1::RButtonClick(TObject *Sender) { ReDrawProbe(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::GButtonClick(TObject *Sender) { ReDrawProbe(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::BButtonClick(TObject *Sender) { ReDrawProbe(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::ReDrawProbe(TObject *Sender) { int r, g, b; r = StrToInt(R->Text); g = StrToInt(G->Text); b = StrToInt(B->Text); ProbeSmall->Canvas->Brush->Color = clWhite; ProbeSmall->Canvas->Rectangle(0, 0, ProbeSmall->Width, ProbeSmall->Height); if (RButton->Checked) { for (int i=0; i<256; i++) { r = i; ProbeSmall->Canvas->Pixels[1][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[2][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[3][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[4][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[5][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[6][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[7][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[8][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[9][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[10][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[11][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[12][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[13][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[14][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[15][i] = (TColor)r + (g << 8) + (b << 16); } } else if (GButton->Checked) { for (int i=0; i<256; i++) { g = i; ProbeSmall->Canvas->Pixels[1][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[2][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[3][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[4][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[5][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[6][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[7][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[8][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[9][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[10][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[11][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[12][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[13][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[14][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[15][i] = (TColor)r + (g << 8) + (b << 16); } } else if (BButton->Checked) { for (int i=0; i<256; i++) { b = i; ProbeSmall->Canvas->Pixels[1][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[2][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[3][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[4][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[5][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[6][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[7][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[8][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[9][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[10][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[11][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[12][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[13][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[14][i] = (TColor)r + (g << 8) + (b << 16); ProbeSmall->Canvas->Pixels[15][i] = (TColor)r + (g << 8) + (b << 16); } } r = StrToInt(R->Text); g = StrToInt(G->Text); b = StrToInt(B->Text); ProbeLarge->Canvas->Brush->Color = clWhite; ProbeLarge->Canvas->Rectangle(0, 0, ProbeLarge->Width, ProbeLarge->Height); if (RButton->Checked) { for (int i=0; i<256; i++) { for (int j=0; j<256; j++) { g = i; b = 255-j; ProbeLarge->Canvas->Pixels[i][255-j] = (TColor)r + (g << 8) + (b << 16); } } } else if (GButton->Checked) { for (int i=0; i<256; i++) { for (int j=0; j<256; j++) { r = i; b = 255-j; ProbeLarge->Canvas->Pixels[i][255-j] = (TColor)r + (g << 8) + (b << 16); } } } else if (BButton->Checked) { for (int i=0; i<256; i++) { for (int j=0; j<256; j++) { r = i; g = 255-j; ProbeLarge->Canvas->Pixels[i][255-j] = (TColor)r + (g << 8) + (b << 16); } } } ReDrawRectangle(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::ReDrawRectangle(TObject *Sender) { int r, g, b; r = StrToInt(R->Text); g = StrToInt(G->Text); b = StrToInt(B->Text); Probe->Canvas->Brush->Color = (TColor)r + (g << 8) + (b << 16); Probe->Canvas->Rectangle(0, 0, Probe->Width, Probe->Height); if (RButton->Checked) { ProbeSmall->Canvas->Ellipse(5, r-3, 13, r+3); ProbeLarge->Canvas->Ellipse(g-3, b-3, g+3, b+3); } else if (GButton->Checked) { ProbeSmall->Canvas->Ellipse(5, g-3, 13, g+3); ProbeLarge->Canvas->Ellipse(r-3, b-3, r+3, b+3); } else if (BButton->Checked) { ProbeSmall->Canvas->Ellipse(5, b-3, 13, b+3); ProbeLarge->Canvas->Ellipse(r-3, g-3, r+3, g+3); } } //--------------------------------------------------------------------------- void __fastcall TForm1::RChange(TObject *Sender) { ReDrawProbe(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::GChange(TObject *Sender) { ReDrawProbe(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::BChange(TObject *Sender) { ReDrawProbe(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::ProbeLargeMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (RButton->Checked) { G->Text = IntToStr(X); B->Text = IntToStr(Y); } else if (GButton->Checked) { R->Text = IntToStr(X); B->Text = IntToStr(Y); } else if (BButton->Checked) { R->Text = IntToStr(X); G->Text = IntToStr(Y); } } //--------------------------------------------------------------------------- void __fastcall TForm1::ProbeSmallMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (RButton->Checked) { R->Text = IntToStr(Y); } else if (GButton->Checked) { G->Text = IntToStr(Y); } else if (BButton->Checked) { B->Text = IntToStr(Y); } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { ReDrawProbe(Sender); Button1Click(Sender); Layers(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Layers() { int r, g, b; TColor color; for (int i=0; i<AllLayers->Width; i++) { for (int j=0; j<AllLayers->Height; j++) { color = AllLayers->Canvas->Pixels[i][j]; r = color & 0xFF; g = (color & 0xFF00) >> 8; b = (color & 0xFF0000) >> 16; LayerR->Canvas->Pixels[i][j] = (TColor)r; LayerG->Canvas->Pixels[i][j] = (TColor)(g << 8); LayerB->Canvas->Pixels[i][j] = (TColor)(b << 16); } } } //---------------------------------------------------------------------------