This is a great candidate for translation into a C primitive.
-- John
Andres Valloud sqrmax@cvtci.com.ar wrote:
Color rendering needs to know about the distance from one color to the other in the RGB space cube. I've been doing this by sending #red, #green and #blue to colors... etc... very slow. So I came up with this method in color that computes the distance squared from one color to an argument in the RGB space cube. It takes 16 microseconds and 691 nanoseconds to execute. I'm not able to speed this anymore in Smalltalk, so I think it can be useful. Measuring the method's execution time, I noticed something that a smallinteger assignment in Squeak's 2.3 VM takes about 90ns to execute (200 mmx). For instance, in the Color>>#distanceTo: method I built, an assignment of the kind
someName _ rgb.
takes 90ns. I'm not saying anything about this. In fact, I think it's pretty fast since it's near memory access time. But maybe it's not. Anyway, here's the method.
Color>>#distanceTo:
distanceTo: aColor "Answer the distance to aColor, ranging from 0.0 to 3.0 in the RGB cube. This is like || self - aColor ||^2. SqR! 2/26/1999 18:03"
| sRGB aRGB blueSummand greenSummand redSummand |
aRGB _ aColor privateRGB. redSummand _ (rgb bitAnd: 16r3FF) - (aRGB bitAnd: 16r3FF). sRGB _ rgb bitShift: -10. aRGB _ aRGB bitShift: -10. greenSummand _ (sRGB bitAnd: 16r3FF) - (aRGB bitAnd: 16r3FF). blueSummand _ (sRGB bitShift: -10) - (aRGB bitShift: -10). ^((redSummand * redSummand) + (greenSummand * greenSummand) + (blueSummand * blueSummand)) asFloat / 1046529.0
Note that 1046529.0 is 1023.0 squared.
Andres.
squeak-dev@lists.squeakfoundation.org