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
 Color approximation algorithm
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

Rocky

3 Posts

Posted - Oct 14 2012 :  09:51:43  Show Profile  Reply
Say that I have a constant array of 16 colors, called Palette:

const
Palette :array[0..15] of TColor = ($F0F0F0, $101010, ... , $202020);

The contents are arbitrary and constant.


I would like to implement a function like this:

function MatchColor(Color :TColor) :integer;

MatchColor accepts *any* Color as a parameter and returns an index into the Palette array that points to the closest color to the one passed into the function. (Note: the Palette array is a global constant and visible to MatchColor)

So, in theory, it could be used like this:

NewColor := Palette[MatchColor(OldColor)];


How would such a function be implemented with ImageEn?

fab

1310 Posts

Posted - Oct 17 2012 :  11:00:43  Show Profile  Reply
ImageEn has the undocumented function _GetSimilColor (in imageenproc unit), which takes an array of TRGB (instead of TColor) values and, making a simple comparison, returns the index of the closest color.
Anyway you may require to compare colors in a different way (for example comparing color tonality, saturation, luminosity or a combination of them): for this task RGB2HSV could be useful.
Go to Top of Page

Rocky

3 Posts

Posted - Oct 18 2012 :  15:18:57  Show Profile  Reply
Thanks man. Sorry but as I am relatively novice in imageen could you please give me an example of GetSimilColor?
Go to Top of Page

fab

1310 Posts

Posted - Oct 19 2012 :  00:15:52  Show Profile  Reply
Look at this:


var
  palette: array [0..2] of TRGB = ((b: 0; g: 0; r: 0), (b: 12; g: 20; r: 30), (b: 40; g: 60; r: 70));

procedure TForm1.Button22Click(Sender: TObject);
var
  RGBToFind: TRGB;
  idx: integer;
begin
  RGBtoFind := CreateRGB(28, 18, 10); // should match index 1 (b:12, g:20, r:30)
  idx := _GetSimilColor(palette, length(palette), RGBtoFind);
  ShowMessage(IntToStr(idx));
end;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: