【问题标题】:Getting the ICO predominant color获得 ICO 的主要颜色
【发布时间】:2011-09-21 20:33:25
【问题描述】:

如何获得图标的主要颜色? 我正在使用delphi 2007

【问题讨论】:

  • 我会遍历图像中的所有像素,看看每种颜色出现了多少次。出现次数最多的颜色是你天真的主要颜色。
  • 你用的是哪个版本的delphi?
  • @Blender:如果图标只使用 16 种颜色,这可能会起作用。现在我认为最好找到平均色调、饱和度和值。
  • @Andreas,好吧,我 确实 说天真;)我没想到它适用于所有情况(棋盘?),但 HSV 会是一个更好的解决方案,我同意。

标签: delphi colors


【解决方案1】:

这是我基于此编写的一些杂乱代码,您应该找到最佳解决方案

type
  TElement = packed record
    ocurrences: Integer;
    color: TColor;
  end;

  Element = ^TElement;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  fname: string;
  i, j, k, max: Integer;
  lista_culori: TList;
  tmp: TColor;
  el: Element;
  este: Boolean;
  test: TIcon;
  bmp: TBitmap;
begin
  lista_culori := TList.Create;
  if PromptForFileName(fname, '', '', '', '', false) then
  begin
    test := TIcon.Create;
    bmp := TBitmap.Create;
    test.LoadFromFile(fname);
    bmp.Height := test.Height;
    bmp.Width := test.Width;
    bmp.Canvas.Draw(0, 0, test);
    test.Free;
    for i := 0 to bmp.Width do
      for j := 0 to bmp.Height do
      begin
        tmp := bmp.Canvas.Pixels[i, j];
        este := false;
        for k := 0 to lista_culori.Count - 1 do
        begin
          if Element(lista_culori[k])^.color = tmp then
          begin
            el := Element(lista_culori[k]);
            el^.ocurrences := el^.ocurrences + 1;
            este := True;
            el := nil;
          end;
        end;
        if not este then
        begin
          GetMem(el, SizeOf(TElement));
          el^.ocurrences := 0;
          el^.color := tmp;
          lista_culori.Add(el);
        end;
      end;
  end;
  max := Element(lista_culori[0])^.ocurrences;
  k := 0;
  for i := 1 to lista_culori.Count - 1 do
  begin
    if max < Element(lista_culori[i])^.ocurrences then
    begin
      k := i;
      max := Element(lista_culori[i])^.ocurrences;
    end;
  end;
  ShowMessage(ColorToString(Element(lista_culori[k])^.color));
end;

【讨论】:

  • 你可以让它忽略背景颜色吗?
  • 您可以对该列表进行排序并采用下一个主要颜色或排除通常 bkcolor 的 clWhite
猜你喜欢
  • 2011-06-07
  • 2016-01-19
  • 2020-06-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多