ImageEn, unit imageenproc |
|
TImageEnProc.GetDominantColor
Declaration
function GetDominantColor(var Color: TRGB): Double; overload;
function GetDominantColor(var Color: TColor): Double; overload;
function GetDominantColor(var Color: TColor; ExcludeColors: array of TColor): Double; overload;
Description
Return the dominant (most common) color in the image.
Color will contain the dominant color, and the result will return the percentage of the image with that color (value range is 0.0 to 100.0).
You can use the ExcludeColors overload to get a list of the dominant colors in an image (see example below).
Notes:
- Works only with ie24RGB and ie1g
pixel formats- It is best not used with
transparent images as colors in transparent areas (that are not visible) will affect the result
Demos
| Demos\ImageAnalysis\Histogram\Histogram.dpr |
| Demos\ImageEditing\EveryMethod\EveryMethod.dpr |
Examples
perc := ImageEnView1.Proc.GetDominantColor(cl);
If Round( perc ) = 100 then
ShowMessage('The image is blank!');
// Show the dominant color of the image
d := ImageEnView1.Proc.GetDominantColor( rgb );
lblDominantColor.Caption := format( 'Dominant Color (%d%%): %s', [ Round( d ), ColorToHex( TRGB2TColor( rgb )) ]);
IEColorButton1.SelectedColor := TRGB2TColor( rgb );
// Get the ten most used colors
var
i: Integer;
usedColors : array[1..10] of TColor;
c: TColor;
dd: Double;
begin
Memo1.Clear;
// Reset used color array
for i := Low(usedColors) to High(usedColors) do
usedColors[i] := clNone;
for i := 1 to 10 do
begin
dd := ImageEnView1.Proc.GetDominantColor( c, usedColors );
usedColors[ i ] := c;
Memo1.Lines.Add( format( '%s (%d%%)', [ ColorToHex( c ), Round( dd ) ]));
end;
// Show palette to users
IEPromptForColor( c, usedColors, 10 );
end;
// Load test image
ImageEnView1.IO.LoadFromFile( 'D:\TestImage.jpg' );

// Return the most common color in the image (and its percentage of the image)
d := ImageEnView1.Proc.GetDominantColor( rgb );
s := format( 'Dominant Color (%d%%): %s', [ Round( d ), ColorToHex( TRGB2TColor( rgb )) ]);
ImageTest1.jpg: Dominant Color (0%): #060606
ImageTest2.jpg: Dominant Color (39%): #FFFFFF
ImageTest3.jpg: Dominant Color (20%): #1C1C1C
See Also
-
CreateRGB-
TRGB2TColor-
TColor2TRGB-
ColorToHex