gpt4 book ai didi

delphi - 以编程方式逐个像素地交换小位图(原始)的颜色

转载 作者:行者123 更新时间:2023-12-02 09:26:59 28 4
gpt4 key购买 nike

在此处下载带有已编译可执行文件的源代码(大小:161 KB(165,230 字节)):http://www.eyeClaxton.com/download/delphi/ColorSwap.zip

原始位图大小仅为 28x15 像素,颜色为浅蓝色。我希望能够单击右侧的任何彩色面板,并将原始位图颜色从浅蓝色更改为面板的颜色。

如果您单击灰色面板,您可以看到它的实际效果,我只是不知道如何使用其他颜色正确执行此操作。任何帮助将不胜感激。如果需要更多信息,请随时询问。

我有asked this question before但我无法弄清楚我想做什么,所以我希望这个能更清楚一点。

alt text

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.

最佳答案

这个问题仍然没有明确定义,因为灰色不像任何其他颜色那样工作(无论是在 RGB 模型中,还是在 HSV 模型中),因此没有单一、明显的方法来实现其他颜色按钮.

但是,一种自然的方式(也许是最自然的方式)是按照我在回答上一个问题时建议的方式进行,即从 HSV(h, s, v) 转换每个像素红色情况下为 HSV(0, s, v),绿色情况下为 HSV(120, s, v),以及 HSV(240, s , v) 在蓝色的情况下。数字 0、120 和 240 是色调的角度。

为此,您只需要在 RGB 和 HSV 之间进行转换的函数(我在上一个问题中确实给了您这些函数)。

我在您的代码中看到您已将函数命名为BlueToRed(Color)等,这是不合适的,因为任何颜色都会变成红色等,所以更好名称将是 ColorToRed 等。

为了尽可能清楚地说明这一点,我在您的程序中添加了红色和绿色按钮的代码。请参阅更新版本:

http://privat.rejbrand.se/ColorSwap.zip

(另外,请注意“ColorSwap”是一个不合适的名称。更好的名称是“FixHue”。)

性能

此外,您可能已经注意到,性能很糟糕!为图像着色需要几秒钟!

这并不是因为 CPU 很慢(事实上,它非常快),而是主要由于两个设计错误:

  1. 切勿更新屏幕上的像素图。相反,更新内存中的像素图,然后,完成后,将位图复制到屏幕。

  2. 请勿使用 Pixels 属性。这速度慢得令人尴尬。请改用扫描线

如果做得正确,您应该能够每秒进行数百次更新...

关于delphi - 以编程方式逐个像素地交换小位图(原始)的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4650421/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com