problem with printHtmlString

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

problem with printHtmlString

Nicolas Cellier
Hi,
while looking at Color, I noticed this (I think that i already wrote about it years ago...):

(Color fromString: '#F40072') printHtmlString ->  'F30071'
it truncates each (10 bit component / 1023.0) * 255

This does not work:

((0 to: 255) reject: [:e | (((e/255) * 1023.0) rounded / 1023.0 * 255) truncated  = e])
= ((1 to: 42) , (86 to: 127) , (171 to: 212)).

Using rounded instead of asInteger (truncated) would fix it though

((0 to: 255) reject: [:e | (((e/255) * 1023.0) rounded / 1023.0 * 255) rounded= e])
= #().

asHTMLColor does it differently
1) truncate 10 bits components to 8 bits (by shifting)
2) print 8 bits components (more exactly print each 4 bits nibble)
(Color fromString: '#F40072') asHTMLColor -> '#F40072'

Note that the last two bits of (10 bits components) are not necessarily set to (00) when specifying the Color via 8 bits component / 255

((0 to: 255) select: [:e | ((e/255) * 1023.0) rounded = (e<<2)])
 = (0 to: 42) asArray.

((0 to: 255) select: [:e | ((e/255) * 1023.0) rounded = (e<<2+2r01)])
 = (43 to: 127) asArray.

((0 to: 255) select: [:e | ((e/255) * 1023.0) rounded = (e<<2+2r10)])
 = (128 to: 212) asArray.

((0 to: 255) select: [:e | ((e/255) * 1023.0) rounded = (e<<2+2r11)])
 = (213 to: 255) asArray.

It also works the same with /255.0.
So truncating like asHTMLColor does, is the right thing and always works.