获得ICO的主要颜色

时间:2011-09-21 20:33:25

标签: delphi colors

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

1 个答案:

答案 0 :(得分: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;