Hi,
Color>>hex in my 3.8 image sends a 'radix indicator not found' error.
This seems to be because SmallInteger>>printStringRadix: puts a "10r" or "-10r" at the start of the printed value when asked for base 10, but doesn't put in a "XXr" when asked for other bases.
The relevant bit of code in Color>>hex does this
Here's SmallInteger>>printStringRadix:
printStringRadix: baseInteger "Return a string containing a sequence of characters that represents the numeric value of the receiver in the radix specified by the argument. If the receiver is negative, a minus sign ('-') is prepended to the sequence of characters. The result is undefined if baseInteger less than 2 or greater than 36." | tempString | #Numeric. "2000/03/04 Harmon R. Added ANSI <integer> protocol" baseInteger = 10 ifTrue: [tempString := self printStringBase: baseInteger. self negative ifTrue: [^ '-10r' , (tempString copyFrom: 2 to: tempString size)] ifFalse: [^ '10r' , tempString]]. ^ self printStringBase: baseInteger
and printStringBase:
printStringBase: base | stream integer next | self = 0 ifTrue: [^'0']. self negative ifTrue: [^'-', (self negated printStringBase: base)]. stream := WriteStream on: String new. integer := self normalize. [integer > 0] whileTrue: [ next := integer quo: base. stream nextPut: (Character digitValue: integer - (next * base)). integer := next]. ^stream contents reversed
So - how to fix. One "low touch" fix is just to get Color>>hex not to fuss if it can't find the "r" that it thinks should be there. But that leaves the strange behaviour of printStringRadix: which can't make up its mind whether to show the radix or not. I don't know what the right behaviour is; the comment in the methd doesn't clarify.
So - what would an experienced Squeaker do?
Thanks, Steve
Hi Steve,
Hi,
Color>>hex in my 3.8 image sends a 'radix indicator not found' error.
This seems to be because SmallInteger>>printStringRadix: puts a "10r" or "-10r" at the start of the printed value when asked for base 10, but doesn't put in a "XXr" when asked for other bases.
What is an example that causes the error?
And - are you talking about Color's class method hex:? e.g.
Color hex: 0.43
I ask in case you have a package loaded I don't, or something of that nature. (I don't see a "hex" instance method on Color in my base 3.8 image, but I did note that the hex: class method eventually calls the kinds of things you're talking about.)
I agree that the printStringRadix: behavior looks kind of funky. I wonder if it's to conform to some quirk of the ANSI standard (only because the comment mentions it), or if it expected printStringBase: to put the "XXr" on there in other cases, or some other thing.
Thanks, Ben Schroeder
On 06/05/06, Benjamin Schroeder benschroeder@acm.org wrote:
What is an example that causes the error?
And - are you talking about Color's class method hex:? e.g.
Color hex: 0.43
I ask in case you have a package loaded I don't, or something of that nature. (I don't see a "hex" instance method on Color in my base 3.8 image, but I did note that the hex: class method eventually calls the kinds of things you're talking about.)
Hi Ben,
Yes - sorry. hex instance method is added by seaside. Looks like so:
hex ^ ((self red * 255) asInteger printPaddedWith: $0 to: 2 base: 16) , ((self green * 255) asInteger printPaddedWith: $0 to: 2 base: 16) , ((self blue * 255) asInteger printPaddedWith: $0 to: 2 base: 16).
Then Integer#printPaddedWith:to:base::
printPaddedWith: aCharacter to: anInteger base: aRadix "Answer the string containing the ASCII representation of the receiver padded on the left with aCharacter to be at least anInteger characters." | aStream padding digits | #Numeric. "2000/03/04 Harmon R. Added Date and Time support" aStream := WriteStream on: (String new: 10). self printOn: aStream base: aRadix showRadix: false. digits := aStream contents. padding := anInteger - digits size. padding > 0 ifFalse: [^ digits]. ^ ((String new: padding) atAllPut: aCharacter; yourself) , digits
Which is how we get to printOn:base:showRadix, which calls printStringRadix:
Steve
Hi Steve,
Yes - sorry. hex instance method is added by seaside. Looks like so:
hex ^ ((self red * 255) asInteger printPaddedWith: $0 to: 2 base: 16) , ((self green * 255) asInteger printPaddedWith: $0 to: 2 base: 16) , ((self blue * 255) asInteger printPaddedWith: $0 to: 2 base: 16).
Ah, I see! I should have checked my Seaside image ... :)
We might be able to make use of Color class>>hex: here in any case ... replacing the Color>>hex method with something like
hex ^ (self class hex: self red), (self class hex: self green), (self class hex: self blue)
might work. I'm of course still not sure why the other method does what it does; #printPaddedWith:to:base: should probably be fixed in any case. Color class>>hex: takes a different route to do the conversion.
This implementation of Color>>hex is terser as well - if it works, I'll submit it as a patch to the Seaside mailing list when I get a chance.
Hope this helps, Ben Schroeder
beginners@lists.squeakfoundation.org