ImageEn, unit imageenproc




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: Integer): Integer; overload;


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.

 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
 You can output the palette as an image using IEDrawColorPalette, or show the palette to the user using IEPromptForColor


Demo  Demos\ImageAnalysis\Histogram\Histogram.dpr
Demo  Demos\ImageEditing\EveryMethod\EveryMethod.dpr


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
  i, cnt: Integer;
  domColors : array of TColor;

  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 );

// Alternative Method to get the ten most used colors
  i: Integer;
  usedColors : array[1..10] of TColor;
  c: TColor;
  dd: Double;

  // Reset used color array
  for i := Low(usedColors) to High(usedColors) do
    usedColors[i] := clNone;

  for i := 1 to 10 do
    dd := ImageEnView1.Proc.GetDominantColor( c, usedColors );
    usedColors[ i ] := c;
    Memo1.Lines.Add( format( '%s (%d%%)', [ ColorToHex( c ), Round( dd ) ]));

  // Show palette to users
  IEPromptForColor( c, usedColors, 10 );

// 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
  domColors: Array of TColor;
  count: Integer;
  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 );


See Also