ImageEn, unit imageenproc

TImageEnProc.GetDominantColor

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;
function GetDominantColor(var DominantColors: array of TColor; Count: Integerr;
                          ExcludeLighterThan: Double = -1;
                          ExcludeDarkerThan: Double = -1): Integer; 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 final overload to get a list of the dominant colors in an image (Ordered from most used downward. Result will be the number of colors returned, which should match Count, except for images with few colors). Ensure you set the length of the array to Count (i.e. using SetLength( DomColors, Count ) before calling the method.
To exclude very light or dark colors (based on HSL lightness), specify values for ExcludeLighterThan (0-255) and/or ExcludeDarkerThan (0-255). Remember that light values will be close to 255 (i.e. white would be exactly 255) and dark values will be close to 0 (i.e. black would be exactly 0).

Note:
Works only with ie24RGB and ie1g pixel formats
Will process either the whole image or a rectangular selection. Other selection types are ignored
It is best not used with transparent images as colors in transparent areas (that are not visible) will affect the result
You can output the palette as an image using IEDrawColorPalette, or show the palette to the user using IEPromptForColor


Demos

Demo  Demos\ImageAnalysis\Histogram\Histogram.dpr
Demo  Demos\ImageEditing\EveryMethod\EveryMethod.dpr
Demo  Demos\ImageEditing\CompleteEditor\PhotoEn.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, cnt: Integer;
  domColors : array of TColor;
  c: TColor;
begin
  Memo1.Clear;

  SetLength( domColors, 10 );
  cnt := ImageEnView1.Proc.GetDominantColor( domColors, 10 );
  if cnt < 10 then
    SetLength( domColors, cnt ); // Image had less than ten colors

  for i := 0 to cnt - 1 do
    Memo1.Lines.Add( ColorToHex( domColors[i] ));

  // Show palette to users
  IEPromptForColor( c, domColors, cnt );
end;

// Alternative Method to 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


// Show in ImageEnView2 the 64 most common colors in the image in ImageEnView1
var
  domColors: Array of TColor;
  count: Integer;
begin
  SetLength( domColors, 64 );
  count := ImageEnView1.Proc.GetDominantColor( domColors, v1 );
  if count < v1 then
    SetLength( domColors, count ); // Image had less than 64 colors (only needed if you want to do something with domColors)
  IEDrawColorPalette( ImageEnView2.IEBitmap, domColors, 30, 30, 8, clWhite, clBlack, count );
  ImageEnView2.Update();
end;

  


See Also

CreateRGB
TRGB2TColor
TColor2TRGB
ColorToHex