I am not C programmer, but I need to understand this because want to do something similar for ahk. I am used to work with php and ahk so some parts of the syntax I understand.

http://paste.ofcode.org/nkaJTrbSag34Dj9gYjAvE3Code:MagickExport MagickBooleanType IsMagickColorSimilar(const MagickPixelPacket *p, const MagickPixelPacket *q) { MagickRealType fuzz, pixel; register MagickRealType scale, distance; if ((p->fuzz == 0.0) && (q->fuzz == 0.0)) return(IsMagickColorEqual(p,q)); if (p->fuzz == 0.0) fuzz=MagickMax(q->fuzz,MagickSQ1_2)*MagickMax(q->fuzz,MagickSQ1_2); else if (q->fuzz == 0.0) fuzz=MagickMax(p->fuzz,MagickSQ1_2)*MagickMax(p->fuzz,MagickSQ1_2); else fuzz=MagickMax(p->fuzz,MagickSQ1_2)*MagickMax(q->fuzz,MagickSQ1_2); scale=1.0; distance=0.0; if ((p->matte != MagickFalse) || (q->matte != MagickFalse)) { /* Transparencies are involved - set alpha distance. */ pixel=(p->matte != MagickFalse ? GetPixelOpacity(p) : OpaqueOpacity)- (q->matte != MagickFalse ? q->opacity : OpaqueOpacity); distance=pixel*pixel; if (distance > fuzz) return(MagickFalse); /* Generate a alpha scaling factor to generate a 4D cone on colorspace. Note that if one color is transparent, distance has no color component */ if (p->matte != MagickFalse) scale=(QuantumScale*GetPixelAlpha(p)); if (q->matte != MagickFalse) scale*=(QuantumScale*GetPixelAlpha(q)); if ( scale <= MagickEpsilon ) return(MagickTrue); } /* CMYK create a CMY cube with a multi-dimensional cone toward black. */ if (p->colorspace == CMYKColorspace) { pixel=p->index-q->index; distance+=pixel*pixel*scale; if (distance > fuzz) return(MagickFalse); scale*=(MagickRealType) (QuantumScale*(QuantumRange-p->index)); scale*=(MagickRealType) (QuantumScale*(QuantumRange-q->index)); } /* RGB or CMY color cube. */ distance*=3.0; /* rescale appropriately */ fuzz*=3.0; pixel=p->red-q->red; if ((p->colorspace == HCLColorspace) || (p->colorspace == HSBColorspace) || (p->colorspace == HSLColorspace) || (p->colorspace == HWBColorspace)) { /* This calculates a arc distance for hue Really if should be a vector angle of 'S'/'W' length with 'L'/'B' forming appropriate cones. In other words this is a hack - Anthony */ if (fabs((double) pixel) > (QuantumRange/2)) pixel-=QuantumRange; pixel*=2; } distance+=pixel*pixel*scale; if (distance > fuzz) return(MagickFalse); pixel=GetPixelGreen(p)-q->green; distance+=pixel*pixel*scale; if (distance > fuzz) return(MagickFalse); pixel=GetPixelBlue(p)-q->blue; distance+=pixel*pixel*scale; if (distance > fuzz) return(MagickFalse); return(MagickTrue); }

Here isdescription to the function:

http://paste.ofcode.org/mfe2Zy7L4h7A8KRAqDGWEE

What I know about the function:

It is function to compare two colors, weather they are in tolerance (fuzz). It is designer for CIALAB/Lab colorspace but it can work also with RGB.

u object contains scaled values in range 0 to 1. u.r is red channel.

After doing the match, it is needed to convert back to normal color range by multiplying by quantumrange (Q16=65535).

I read about the formula for this function:

sqrt((reddiff^2+greendiff^2+bluediff^2)/3)

This implements the equivalent of...

fuzz < sqrt( color_distance^2 * u.a*v.a + alpha_distance^2 )

For example for an RGB

color_distance^2 = ( (u.r-v.r)^2 + (u.g-v.g)^2 + (u.b-v.b)^2 ) / 3

so fuzz=color_distance if only RGB and no alpha

However, you can apply the same formula in normal Q range if you want using each color in the range 0 to quantumrange or 0 to 255 (however, you want to see the values) or even multiply by 100 for percent.

My problem is that I am not good on math, but maybe you can help me to understand some C commands esspecialy on the begin of the function, it's definition on begin and arguments:

please explain me the arguments - meaning of

Code:MagickPixelPacket *p, const MagickPixelPacket *qCode:MagickRealType fuzz, pixel;Code:register MagickRealType scale, distance;Also this functions and constants are in the code...Code:static inline double

Code:#define MagickSQ1_2 0.70710678118654752440084436210484903928483593768847 static inline double MagickMax(const double x,const double y) { if (x > y) return(x); return(y); } static inline MagickRealType RadiansToDegrees(const MagickRealType radians) { return((MagickRealType) (180.0*radians/MagickPI)); }I tried to find out where MagickRealTypeCode:MagicPixel packed is defined here: typedef struct _MagickPixelPacket { ClassType storage_class; ColorspaceType colorspace; MagickBooleanType matte; double fuzz; size_t depth; MagickRealType red, green, blue, opacity, index; } MagickPixelPacket;

is defined in source files but no success.

Tweet This+ 1 thisPost To Linkedin