Delphi 德尔菲-实施;乌贼墨“;用于任意颜色的例程

Delphi 德尔菲-实施;乌贼墨“;用于任意颜色的例程,delphi,colors,image-manipulation,delphi-2006,colorize,Delphi,Colors,Image Manipulation,Delphi 2006,Colorize,我见过将图像转换为深褐色版本的奇怪例程,如下所示: function bmptosepia(const bmp: TBitmap; depth: Integer): Boolean; var color,color2:longint; r,g,b,rr,gg:byte; h,w:integer; begin for h := 0 to bmp.height do begin for w := 0 to bmp.width do begin //first convert

我见过将图像转换为深褐色版本的奇怪例程,如下所示:

function bmptosepia(const bmp: TBitmap; depth: Integer): Boolean;
var
color,color2:longint;
r,g,b,rr,gg:byte;
h,w:integer;
begin
  for h := 0 to bmp.height do
  begin
    for w := 0 to bmp.width do
    begin
//first convert the bitmap to greyscale
    color:=colortorgb(bmp.Canvas.pixels[w,h]);
    r:=getrvalue(color);
    g:=getgvalue(color);
    b:=getbvalue(color);
    color2:=(r+g+b) div 3;
    bmp.canvas.Pixels[w,h]:=RGB(color2,color2,color2);
//then convert it to sepia
    color:=colortorgb(bmp.Canvas.pixels[w,h]);
    r:=getrvalue(color);
    g:=getgvalue(color);
    b:=getbvalue(color);
    rr:=r+(depth*2);
    gg:=g+depth;
    if rr <= ((depth*2)-1) then
    rr:=255;
    if gg <= (depth-1) then
    gg:=255;
    bmp.canvas.Pixels[w,h]:=RGB(rr,gg,b);
    end;
  end;
end;

(我不知道为什么原稿是作为布尔函数编写的)。

您的基本算法只是向灰度值的每个颜色通道添加固定偏移量。我们可以对此进行概括,以便
NewColor
参数确定每个通道的偏移量。请注意,
depth
变得多余,您可以完全忽略它

rbase:=getrvalue(NewColor);
gbase:=getgvalue(NewColor);
bbase:=getbvalue(NewColor);
base:=min(rbase,min(gbase,bbase));
rdepth:=rbase-base;
gdepth:=gbase-base;
bdepth:=bbase-base;

rr:=r+rdepth;
gg:=g+gdepth;
bb:=b+bdepth;
if rr < rdepth then
    rr:=255;
if gg < gdepth then
    gg:=255;
if bb < bdepth then
    bb:=255;
rbase:=getrvalue(NewColor);
gbase:=getgvalue(NewColor);
bbase:=getbvalue(NewColor);
基数:=min(rbase,min(gbase,bbase));
rdepth:=rbase base;
gdepth:=gbase-base;
b深度:=b碱基;
rr:=r+rdepth;
gg:=g+gdepth;
bb:=b+b深度;
如果rr
此外,请使用而不是。当使用更多像素时,后者的效率非常低。
rbase:=getrvalue(NewColor);
gbase:=getgvalue(NewColor);
bbase:=getbvalue(NewColor);
base:=min(rbase,min(gbase,bbase));
rdepth:=rbase-base;
gdepth:=gbase-base;
bdepth:=bbase-base;

rr:=r+rdepth;
gg:=g+gdepth;
bb:=b+bdepth;
if rr < rdepth then
    rr:=255;
if gg < gdepth then
    gg:=255;
if bb < bdepth then
    bb:=255;