透明光标在 Delphi 中显示错误的颜色

Transparent cursor shows wrong color in Delphi

我想绘制一个自定义的半透明光标,它是一个白色圆圈。我在 Windows 7 OS 中使用 Delphi XE3。

我的代码在某种程度上可以正常工作。我可以设置 alpha 通道并修改透明度,但颜色是 silver/dark 灰色而不是白色。

当我 运行 调试时,我可以看到正在设置正确的白色 ($B4FFFFFF),其中 $B4 是 alpha 值。

我已经根据之前的 subject

改编了我的代码
type
pScanLineArray = ^TScanLineArray;
TScanLineArray= array [Word] of Cardinal;

procedure TForm1.Button1Click(Sender: TObject);
  const CursorIdx = TCursor(-25); {arbitrary}
        Mid = 64; {middle of cursor dimension (128)}
        FPenSize = 50;
  var IconInfo : TIconInfo;
      X, Y: Integer;
      AlphaCursor: HCURSOR;
      ForG, BackG, Border: Cardinal;
      Dist: Double;
      LineP: pScanLineArray;
      FCursorMaskBMP,FCursorColorBMP: TBitmap;
begin
  {Create Bitmaps}
  FCursorMaskBMP:= TBitMap.Create;
  FCursorMaskBMP.PixelFormat:= pf32bit;
  FCursorMaskBMP.SetSize(128, 128);

  FCursorColorBMP:= TBitMap.Create;
  FCursorColorBMP.PixelFormat:= pf32bit;
  FCursorColorBMP.SetSize(128, 128);


  {Fill the AND mask. In this case, content ignored by windows}
  FCursorMaskBMP.Monochrome := True;
  FCursorMaskBMP.Canvas.Brush.Color := clWhite;
  FCursorMaskBMP.Canvas.FillRect(FCursorMaskBMP.Canvas.ClipRect);

  {Set all the colors for the cursor}
  ForG:= $B4FFFFFF; {semi-transarent white - for inside of circle}
  BackG:= [=11=]000000; {Full transparent - for outside of circle}
  Border:= $FF000000; {non-transparent black for border}

  {Fill the bitmap}
  for Y := 0 to (FCursorColorBMP.Height - 1) do
  begin
    LineP := FCursorColorBMP.ScanLine[Y];
    for X := 0 to (FCursorColorBMP.Width - 1)do
    begin
      Dist:= SQRT(Power(Y-Mid,2)+Power(X-Mid,2)); {Distance from middle of circle}
      if Dist<FPenSize then  {Within circle - apply forground trasparent color}
        LineP^[X] := ForG
      else
        if (Dist>=FPenSize) and (Dist<FPenSize+1) then
          LineP^[X] := Border {This is the border - apply opaque color of border}
        else
          LineP^[X] := BackG; {Outside circle - apply fully transparent color }
    end;
  end;

  FCursorColorBMP.AlphaFormat := afDefined;  {does not seem to have any effect ?}

  with IconInfo do
  begin
    fIcon := false;
    xHotspot := Mid;
    yHotspot := Mid;
    hbmMask := FCursorMaskBMP.Handle;
    hbmColor := FCursorColorBMP.Handle;
  end;

  AlphaCursor:=CreateIconIndirect(IconInfo);
  Win32Check(Bool(AlphaCursor));   {My guess is that this is some error checking ?}
  Screen.Cursors[CursorIdx] := AlphaCursor;
  Screen.Cursor := CursorIdx;

end;

注意:要编译以上内容,必须在"uses"子句

中添加单元"Math"

请看附件

在这种特殊情况下,光标的结果颜色(图像白色上方的部分)是 $xxCACACA

我也试过白色以外的其他颜色。结果相似 - 颜色变得 "darkened"

如果我将 alpha 通道设置为不透明,那么我会得到一个预期的白色光标(但当然不再透明)。

我猜我看到的是光标的白色被混合两次的结果:

  1. 与 black/dark 背景混合(也许是 AND 遮罩?)

  2. 与屏幕图像融为一体。

但这只是一个猜测,我不知道从这里去哪里。有人可以帮忙吗?


关于

的更新
FCursorColorBMP.AlphaFormat := afDefined;

当代码中有上述行时,我得到以下结果

如果抓取颜色值(使用 MS 绘图),光标与不同颜色的重叠会产生这些值 (RGB):

white overlap: 164, 164, 164
Red overlap  : 157, 97,  100
Green overlap: 89,  127, 89
Blue overlap : 89,  89,  164  

如果我忽略上述行或将其更改为

FCursorColorBMP.AlphaFormat := afIgnored;

然后我得到这张图片

当我抓取颜色时,我得到以下值:

white overlap: 202, 202, 202
Red overlap  : 197, 135, 138
Green overlap: 127, 165, 127
Blue overlap : 127, 127, 202 

看到 Tom B运行berg 的回答后,我想我会制作一个 exe 文件并 运行 它在不同的机器上。结果很好,与汤姆的结果一致。

所以总而言之,Alphaformat 应该设置为 AfIgnored(或省略行),这在技术上解决了某些系统上的问题。

出于某种原因,我的系统仍然显示错误的颜色,但是! 我的 Win7 运行s 几乎是通过 Mac OS 上的 Parallels - 不知道这是否是奇怪行为的原因。

按原样应用您的代码时,我得到以下结果:

但是,如果我改变

FCursorColorBMP.AlphaFormat := afIgnored;  {does not seem to have any effect ?}

我得到:

由于 afIgnored 是默认值,您可以简单地省略该行,正如 Sertac Akyuz 在他的评论中所建议的那样。

如果您觉得白色太不透明,请降低 alpha 值。


编辑,更多信息:

虽然我没有找到它的任何文档,但我非常有信心 Windows 在渲染游标时使用 alpha 值,因此不会预先计算位图。为了支持我的想法,我用纯红色、绿色和深蓝色的几种颜色做了以下检查:

alpha 混合的一般公式是

  output = alpha * foreground + (1-alpha) * background

红、绿、蓝分量分别计算。

使用上面的白色叠加层和 alpha 值 (A:B4, R:FF, G:FF, B:FF) 叠加层的颜色分量

 - red field   (R:FF, G:0,  B:0)  becomes R:FF, G:B4, B:B4.
 - green field (R:0,  G:BF, B:0)  becomes R:B4, G:EC, B:B4. 
 - blue field  (R:0,  G:0,  B:7F) becomes R:B4, G:B4, B:D9. 

以上值是使用 "eyedropper" 图形编辑程序获得的。

计算的值相同
  // Result color components
  resR := a * fgR div $FF + ($FF - a) * bgR div $FF;
  resG := a * fgG div $FF + ($FF - a) * bgG div $FF;
  resB := a * fgB div $FF + ($FF - a) * bgB div $FF;

哪里

  // overlay alpha
  a := $B4;
  // overlay color components
  fgR := $FF;
  fgG := $FF;
  fgB := $FF;

  // background color components for the red field
  bgR := $FF;
  bgG := [=15=];
  bgB := [=15=];
  // background color components for the green field
  bgR := [=15=];
  bgG := $BF;
  bgB := [=15=];
  // background color components for the dark blue field
  bgR := [=15=];
  bgG := [=15=];
  bgB := F;

将新创建的位图的bitmap.AlphaFormat设置为afDefinedafPremultiplied(默认为afIgnored)时执行预计算。 (参见 Vcl.Graphics 中的 procedure TBitmap.SetAlphaFormat(Value: TAlphaFormat);