我见过将图像转换为棕褐色版本的奇怪例程,如下所示:
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;
(来自 here )但我需要对任意颜色执行此操作的东西 - 即它将获取图像,大概形成它的灰度版本,然后将新颜色应用于图像。这是我遇到的最后一点麻烦 - 即用感兴趣的颜色替换灰色阴影。
所以我需要
procedure BmpToOneColor (const bmp : TBitmap ;
depth : Integer ;
NewColor : TColor) ;
(我不知道为什么原始写成 bool 函数)。
最佳答案
您的基本算法只是向灰度值的每个颜色 channel 添加固定偏移量。我们可以概括这一点,以便 NewColor 参数确定每个 channel 的偏移量。请注意,深度
变得多余,您可以完全忽略它。
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;
关于Delphi - 任意颜色的 "sepia"例程的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13481305/