[ENH] Color>>#distanceTo:

Andres Valloud sqrmax at cvtci.com.ar
Mon Mar 8 00:51:09 UTC 1999


Hi.

By removing the normalized to 3 floating point answer (which for comparison
reasons is pretty useless), I saved about 7 microseconds. Execution time is now
around 9mus 742ns.

distanceTo: aColor
	"Answer the distance to aColor, ranging
	from 0 to 3139587 in the RGB cube. This is
	like || self - aColor ||^2. SqR! 3/7/1999 21:26"

	| aRGB blueSummand greenSummand redSummand |

	aRGB _ aColor privateRGB.
	redSummand _ (rgb bitAnd: 16r3FF) - (aRGB bitAnd: 16r3FF).
	greenSummand _ ((rgb bitShift: -10) bitAnd: 16r3FF) - ((aRGB bitShift: -10)
bitAnd: 16r3FF).
	blueSummand _ (rgb bitShift: -20) - (aRGB bitShift: -20).
	^(redSummand * redSummand) + 
		(greenSummand * greenSummand) + 
		(blueSummand * blueSummand)

Furthermore, I produced another method for measuring colors against 5 bit per
gun bitmap color entries without having to create colors in the middle. It
executes in about 9mus 654ns. Notice how the extra assignment in the previous
method increases execution time by 90ns :).

distanceTo5bit: anInteger
	"Answer the distance to anInteger, ranging
	from 0 to 3139587 in the RGB cube. This is
	like || self - aColor ||^2. SqR! 3/7/1999 21:29.

	anInteger is [5 bits red][5 bits green][5 bits blue]
	"

	| blueSummand greenSummand redSummand |

	redSummand _ (rgb bitAnd: 16r3FF) - ((anInteger bitAnd: 16r1F) bitShift: 5).
	greenSummand _ ((rgb bitShift: -10) bitAnd: 16r3FF) - (anInteger bitAnd:
16r3E0).
	blueSummand _ (rgb bitShift: -20) - ((anInteger bitAnd: 16r7C00) bitShift: -5).
	^(redSummand * redSummand) + 
		(greenSummand * greenSummand) + 
		(blueSummand * blueSummand)

Andres.





More information about the Squeak-dev mailing list