'From Squeak3.8alpha of ''17 July 2004'' [latest update: #6271] on 27 September 2004 at 1:49:24 pm'! "Change Set: ColorEnh-st Date: 27 September 2004 Author: Samuel Tardieu Color tests were failing: the hex triplet was returned in lower case instead of the expected upper case (test fixed). Also, asHTMLColor returned a string of length 8 by using directly the hex value which encodes a color between 0 and 1023 instead of the expected 0 and 255. The asHTMLColor method has been fixed and tests added. The colorFrom: class method has been enhanced to accept a 6 characters long hexadecimal string."! !Color methodsFor: 'conversions' stamp: 'st 9/27/2004 13:42'! asHTMLColor ^ '#', (self class hex: self red), (self class hex: self green), (self class hex: self blue)! ! !Color class methodsFor: 'instance creation' stamp: 'st 9/27/2004 13:45'! colorFrom: parm "Return an instantiated color from parm. If parm is already a color, return it, else return the result of my performing it if it's a symbol or, if it is a list, it can either be an array of three numbers, which will be interpreted as RGB values, or a list of symbols, the first of which is sent to me and then the others of which are in turn sent to the prior result, thus allowing entries of the form #(blue darker). Else just return the thing" | aColor firstParm | (parm isKindOf: Color) ifTrue: [^ parm]. (parm isKindOf: Symbol) ifTrue: [^ self perform: parm]. (parm isKindOf: String) ifTrue: [^ self fromString: parm]. ((parm isKindOf: SequenceableCollection) and: [parm size > 0]) ifTrue: [firstParm := parm first. (firstParm isKindOf: Number) ifTrue: [^ self fromRgbTriplet: parm]. aColor := self colorFrom: firstParm. parm doWithIndex: [:sym :ind | ind > 1 ifTrue: [aColor := aColor perform: sym]]. ^ aColor]. ^ parm " Color colorFrom: #(blue darker) Color colorFrom: Color blue darker Color colorFrom: #blue Color colorFrom: #(0.0 0.0 1.0) "! ! !Color class methodsFor: 'other' stamp: 'st 9/27/2004 13:41'! hex: aFloat "Return an hexadecimal two-digits string between 00 and FF for a float between 0.0 and 1.0" | str | str := ((aFloat * 255) asInteger hex allButFirst: 3) asLowercase. str size = 1 ifTrue: [^'0',str] ifFalse: [^str]! ! !ColorTest methodsFor: 'testing' stamp: 'st 9/27/2004 13:43'! testAsHTMLColor self assert: (Color white asHTMLColor = '#ffffff'). self assert: (Color black asHTMLColor = '#000000').! ! !ColorTest methodsFor: 'testing' stamp: 'st 9/27/2004 13:45'! testColorFrom self assert: ((Color colorFrom: #white) asHTMLColor = '#ffffff'). self assert: ((Color colorFrom: #(1.0 0.5 0.0)) asHTMLColor = '#ff7f00'). self assert: ((Color colorFrom: (Color white)) asHTMLColor = '#ffffff'). self assert: ((Color colorFrom: '#FF8800') asHTMLColor = '#ff8800').! ! !ColorTest methodsFor: 'testing' stamp: 'st 9/27/2004 13:43'! testFromString self assert: ((Color fromString: '#FF8800') asHTMLColor = '#ff8800').! !