ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Most dominant colors? Most contrasting color?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

PeterPanino

860 Posts

Posted - Sep 12 2017 :  13:51:25  Show Profile  Reply
Hello!

• How can I get the eight most dominant colors from an image?

• And how can I get the most contrasting color for each of these dominant colors? (By contrasting color I mean to get the best readability of a text color where the background is one of the dominant colors).

Are there any such routines already written by anybody?

w2m

USA
1990 Posts

Posted - Sep 12 2017 :  14:42:12  Show Profile  Reply
Here are three different ways to get the contrasting color of a given color:
function ContrastingColor(const AColor: TColor): TColor;
{ Return contracting color of the passed color. }
begin
  if ((AColor and $FF) * 77 + ((AColor shr 8) and $FF) * 150 +
    ((AColor shr 16) and $FF) * 29) > 127 * 256 then
    Result := clBlack
  else
    Result := clWhite;
end;

function GetContrastingColor(AColor: TColor): TColor;
{ Get a contrasting color. }
begin
  AColor := ColorToRGB(AColor); { convert system-defined colors }
  Result := (AColor + $000080) and $0000FF + { red component }
    (AColor + $008000) and $00FF00 + { green component }
    (AColor + $800000) and $FF0000; { blue component }
end;

function GetContrastColor(AColor: TColor): TColor;
{ Get contrast color. }
begin
  AColor := ColorToRGB(AColor); { convert system-defined colors }
  if (AColor + AColor shr 8 + AColor shr 16) and $0000FF >= $180 then
    Result := clBlack
  else
    Result := clWhite;
end;

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

PeterPanino

860 Posts

Posted - Sep 12 2017 :  14:51:25  Show Profile  Reply
The first question could not have a sensible simple single-color answer, as what creates the human impression of the most dominant color in an image may be is rather a range or a bandwidth of colors.

And this range is always a function of the extent of the total uniformity of colors in the image, so it would be relative and not absolute.

So the most dominant color in an image would be the average of a color range which itself is a function of the color uniformity of the image.
Go to Top of Page

PeterPanino

860 Posts

Posted - Sep 15 2017 :  06:34:04  Show Profile  Reply
Bill,

I made a test with your contrast-color functions:



The screenshot contains 6 rows, where the left-most field is a random input color and the smaller fields on the right side are the respective results of your functions.

You can see that ContrastingColor gets either White or Black, GetContrastingColor gets a somewhat contrasting color and GetContrastColor returns ALWAYS White.

So, are these functions based on a scientific color model?
Go to Top of Page

w2m

USA
1990 Posts

Posted - Sep 15 2017 :  12:26:52  Show Profile  Reply
I use the contrasting color functions to make sure any text on or in the image is readable. I do not have any other functions. Try http://www.stackoverflow.com

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: