color matching for pattern


Re: color matching for pattern
#3
Yes, I think that is what I am looking for.

Given the current code is like this
taken from bitsticker project Wrote:#
# bs_matchRGB
#
# Identify the most similar color in the palette to a specified color based
# on Cartesian distance between RGB colorspace points. Derived from libgd's
# gdImageColorClosestAlpha function (in gd.c).
#
# Parameters:
# p, reference to color palette
# r, red value
# g, green value
# b, blue value
#
# Return:
#
#
sub bs_matchRGB {
my $p = shift;
my $r = shift;
my $g = shift;
my $b = shift;

my $key;
my ($dr, $dg, $db);
my $dist;
my $code = DEFAULT_COLOR;

# max potential difference from 0,0,0 black to 255,255,255 white is
# sqrt(195075), or ~442, so start with a mindist greater than the max
# this simplifies the comparison logic so we don't have to test for a first
# case that occurs only once. Maximum RGB color values of 255 are assumed.
my $mindist = 195076;

foreach $key (keys %{$p}) {

# difference between palette and pixel color values
$dr = @{$$p{$key}}[0] - $r;
$dg = @{$$p{$key}}[1] - $g;
$db = @{$$p{$key}}[2] - $b;

# no need to sqrt since comparing the squares yields the same results
$dist = ($dr * $dr) + ($dg * $dg) + ($db * $db);

# is this the closest color yet?
if ($dist < $mindist) {
$mindist = $dist;
$code = $key;
}
}

return $code;
}

I only need to incorporate your pseudo code into this one Smile (hopefully i am not too stupid).

Thanks so far for your answer. I hope this will solve some (or all) of my current issues Smile
Reply
« Next Oldest | Next Newest »



Messages In This Thread
Re: color matching for pattern - by Tim Gould - 2014-01-01, 21:25
Re: color matching for pattern - by Michael Heidemann - 2014-01-01, 21:41
Re: color matching for pattern - by Tim Gould - 2014-01-01, 22:32
Re: color matching for pattern - by Tim Gould - 2014-01-02, 21:43

Forum Jump:


Users browsing this thread: 3 Guest(s)