Delphi - 任意颜色的 "sepia"例程的实现

标签 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 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/

相关文章:

image - 使用 FFMpeg 覆盖时使用调色板

asp.net-mvc - 如何使用 .Net 创建 Facebook 风格的缩略图选择器

delphi - 在 if/else 和 try/finally block 中使用分号

multithreading - 超过 400 个连接的 Indy TCP 服务器

android - 为折线图占据的区域设置特定颜色

javascript - 在 jQuery 中组合图像旋转和裁剪

android - 手势不适用于 Android 上的 ListView 项目

delphi - 压缩并发送 TFDDataset 的数据后释放 Form 时的 AV

java - jPopUpMenu 更改鼠标悬停背景

c++ - 用煤渣转换为灰度