Reputation: 705
How can I get the icon predominant color? I am using delphi 2007
Upvotes: 2
Views: 215
Reputation: 11768
Here is some messy code i crafted based on this you should find a optimal solution
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;
Upvotes: 1