[Tutor] Mad Villain Seeks Source Review.

Tesla Coil tescoil@irtc.net
Mon, 20 Aug 2001 21:45:23 -0500


On 20 August 2001, Ignacio Vazquez-Abrams wrote:
>>>     colorsearch = '[=][\s]?['+colorsearch+']'
>>
>> That RE is _Wrong_. Try "'=\s?\'(%s)\'' % colorsearch".
>
> Turns out that that wasn't the only problem. Here, have fun.

I was beginning to see that.  Altering the original 
just to account for quotation marks appears only to
grab up to the first alphabetic character (?)

>>> import re
>>> def namedict():
>>> # snip
>>>
>>> def firstpass(line):
...     namedcolors = namedict()
...     colors = namedcolors.keys()
...     crush = lambda x,y: x+'|'+ y
...     colorsearch = reduce(crush, colors)
...     colorsearch = '[=][\s]?["|\']?['+colorsearch+']["|\']?'
...     colorsearch = re.compile(colorsearch, re.I)
...     hits = re.findall(colorsearch, line)
...     print hits
... 
>>> firstpass("<BODY BGCOLOR='Magenta'TEXT= yellow>")
["='M", '= y']

This is a radical enough overhaul to be
somewhat demoralizing:

> def namerepl(match):
>     global namedcolors
>     a,b,c,d=match.group(1,2,3,4)
>     t=d or c
>     return "='#%s'" % namedcolors[t.lower()]
>
> def firstpass(line):
>     global namedcolors
>     colors='|'.join(namedcolors.keys())
>     colorsearch='=\s*((\'(%s)\')|(%s))' % (colors, colors)
>     colorre=re.compile(colorsearch, re.I)
>     line=colorre.sub(namerepl, line)
>     return line

And this is thoroughly depressing...

> http://developer.netscape.com/docs/manuals/htmlguid/colortab.htm

*Sigh*, If it were simply a matter of rgb values:

def grayscale(rgbstring):
    r = rgbstring[0:2]
    g = rgbstring[2:4]
    b = rgbstring[4:6]
    hexcon = lambda x: float(eval('0x'+x))
    vl = map(hexcon, [r,g,b])
    grayrgb = int((0.3*vl[0])+(0.59*vl[1])+(0.11*vl[2]))
    grayrgb = str(hex(grayrgb))
    grayrgb = grayrgb[2:]*3
    return grayrgb