Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically swap colors from a small bitmap (original), pixel by pixel
    text
    copied!<p>Download the source code with compiled executable here (Size: 161 KB (165,230 bytes)): <a href="http://www.eyeClaxton.com/download/delphi/ColorSwap.zip" rel="nofollow noreferrer">http://www.eyeClaxton.com/download/delphi/ColorSwap.zip</a></p> <p>Original bitmap size is just 28x15 pixels, and the color is light blue. I would like to be able to click on any of the colored panels to the right and change the original bitmap color from light blue to the color of the panel.</p> <p>If you click on the gray panel you can see this in action, I just can't figure out how to do this correctly with the other colors. Any help would be greatly appreciated. If more information is needed, please feel free to ask.</p> <p>I have <a href="https://stackoverflow.com/questions/4642521">asked this question before</a> but I was unable to make clear what I was trying to do, so I hope this one is a little bit more clear.</p> <p><img src="https://i.stack.imgur.com/WWoPD.png" alt="alt text"></p> <pre><code>unit MainUnit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls; type TMainFrm = class(TForm) Panel1: TPanel; Label1: TLabel; Panel2: TPanel; Label2: TLabel; BeforeImage1: TImage; AfterImage1: TImage; Panel3: TPanel; Panel4: TPanel; Panel5: TPanel; Panel6: TPanel; Panel7: TPanel; Panel8: TPanel; Panel9: TPanel; Image1: TImage; Label3: TLabel; Panel10: TPanel; Memo1: TMemo; Label4: TLabel; procedure FormCreate(Sender: TObject); procedure Panel4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var MainFrm: TMainFrm; implementation uses Math; {$R *.DFM} function Min(const A, B, C: Integer): Integer; begin Result := Math.Min(A, Math.Min(B, C)); end; function Max(const A, B, C: Integer): Integer; begin Result := Math.Max(A, Math.Max(B, C)); end; function RGBToGray(theRed, theGreen, theBlue: Byte): Byte; begin Result := (Max(theRed, theGreen, theBlue) + Min(theRed, theGreen, theBlue)) div 2; end; function BlueToGray(theColor: TColor): TColor; var R, G, B, X: Byte; begin R := (theColor and $FF); G := (theColor and $FF00) shr 8; B := (theColor and $FF0000) shr 16; X := RGBToGray(R, G, B); Result := TColor(RGB(X, X, X)); end; procedure TMainFrm.FormCreate(Sender: TObject); begin Image1.Picture.Graphic := BeforeImage1.Picture.Bitmap; end; procedure TMainFrm.Panel4Click(Sender: TObject); var Bitmap: TBitmap; I, X: Integer; Color: Integer; begin Bitmap := TBitmap.Create; try Bitmap.Assign(BeforeImage1.Picture.Bitmap); Panel4.Caption := ''; Panel5.Caption := ''; Panel6.Caption := ''; Panel7.Caption := ''; Panel8.Caption := ''; Panel9.Caption := ''; (Sender as TPanel).Caption := 'X'; for X := 0 to (Bitmap.Height - 1) do begin for I := 0 to (Bitmap.Width - 1) do begin Color := Bitmap.Canvas.Pixels[I, X]; case (Sender as TPanel).Tag of 1: ; // I need a function something like BlueToRed(Color); 2: ; // I need a function something like BlueToGreen(Color); 3: ; // I need a function something like BlueToYellow(Color); 4: ; // I need a function something like BlueToFuchsia(Color); 5: ; // I need a function something like BlueToCyan(Color); 6: Bitmap.Canvas.Pixels[I, X] := BlueToGray(Color); end; Image1.Picture.Graphic := Bitmap; end; Application.ProcessMessages(); Sleep(100); end; AfterImage1.Picture.Graphic := Bitmap; finally Bitmap.Free; end; end; end. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload