Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## [Webware-checkins] r5631 - Webware/trunk/WebKit/Examples

 [Webware-checkins] r5631 - Webware/trunk/WebKit/Examples From: - 2006-08-22 23:44:20 ```Author: chrisz Date: Tue Aug 22 17:44:13 2006 New Revision: 5631 Modified: Webware/trunk/WebKit/Examples/Colors.py Webware/trunk/WebKit/Examples/FileUpload.py Log: More cosmetic on Examples Modified: Webware/trunk/WebKit/Examples/Colors.py ============================================================================== --- Webware/trunk/WebKit/Examples/Colors.py (original) +++ Webware/trunk/WebKit/Examples/Colors.py Tue Aug 22 17:44:13 2006 @@ -1,75 +1,90 @@ import os from ExamplePage import ExamplePage + +# Helper functions + +gamma = 2.2 # an approximation for today's CRTs + +def brightness(r, g, b): + """Calculate brightness of RGB color.""" + r, g, b = map(lambda x: x/255.0, (r, g, b)) + return (0.3*r**gamma + 0.6*g**gamma + 0.1*b**gamma)**(1/gamma) + +def textcolor(r, g, b): + """Determine a good text font color for high contrast.""" + return brightness(r, g, b) < 0.5 and 'white' or 'black' + +def RGBToHTMLColor(r, g, b): + """Convert r, g, b to #RRGGBB.""" + return '#%02X%02X%02X' % (r, g, b) + +def HTMLColorToRGB(h): + """Convert #RRGGBB to r, g, b.""" + h = h.strip() + if h.startswith('#'): + h = h[1:] + h = h[:2], h[2:4], h[4:] + return map(lambda x: int(x, 16), h) + +# Prepare HTML for color table + +numSteps = 6 # this gives the "web-safe" color palette +steps = map(lambda x: 255.0*x/(numSteps-1), range(numSteps)) + +colorTable = [ + '

Click on one of the colors below to set the background color.

', + ''] +for r in steps: + for g in steps: + colorTable.append('
%s
\n') + for b in steps: + color = RGBToHTMLColor(r, g, b) + colorTable.append('\n' + % (color, textcolor(r, g, b), color, color)) + colorTable.append('\n') +colorTable.append('') +colorTable = ''.join(colorTable) + + class Colors(ExamplePage): """Colors demo. This class is a good example of caching. The color table that - this servlet creates never changes, so the servlet caches this in - the _htColorTable attribute. The original version of this + this servlet creates never changes, so the servlet caches this + in the global colorTable variable. The original version of this example did no caching and was 12 times slower. """ - def __init__(self): - ExamplePage.__init__(self) - self._htColorTable = None - - def awake(self, trans): - """Set _bgcolor according to our field.""" - ExamplePage.awake(self, trans) - self._bgcolor = '' - self._bgcolorArg = '' - req = self.request() - if req.hasField('bgcolor'): - self._bgcolor = req.field('bgcolor').strip() - def htBodyArgs(self): - """Overridden to throw in the custom background color that the user can specify in our form.""" - bgcolor = self._bgcolor or 'white' - return 'color="black" bgcolor="%s" ' \ - 'style="color:black;background-color:%s"' % ((bgcolor,)*2) + """Write the attributes of the body element. + + Overridden in order to throw in the custom background color + that the user can specify in our form. + + """ + self._bgcolor = self.request().field('bgcolor', '#FFFFFF') + try: + r, g, b = HTMLColorToRGB(self._bgcolor) + self._color = textcolor(r, g, b) + except: + self._color = 'black' + return 'text="black" bgcolor="%s" style="background-color:%s"' \ + % ((self._bgcolor,)*2) def writeContent(self): - self.writeln('
') + """Write the actual content of the page.""" self.write(''' +

Color Table Demo

- Background color: + Background color: - ''' % (self._bgcolor)) - self.writeln(self.htColorTable()) - self.writeln('
') - - def htColorTable(self): - if self._htColorTable is None: - colorTable = [''] - gamma = 2.2 # an approximation for today's CRTs, see "brightness =" below - numSteps = 8 - steps = map(float, range(numSteps)) - denominator = float(numSteps-1) - for r in steps: - r = r/denominator - for g in steps: - g = g/denominator - colorTable.append('
%s
\n') - for b in steps: - b = b/denominator - color = '#%02x%02x%02x' % (r*255, g*255, b*255) - # Compute brightness given RGB: - brightness = (0.3*r**gamma + 0.6*g**gamma + 0.1*b**gamma)**(1/gamma) - # We then use brightness to determine a good font color for high contrast: - if brightness < 0.5: - textcolor = 'white' - else: - textcolor = 'black' - colorTable.append('\n' - % (color, textcolor, color, color)) - colorTable.append('\n') - colorTable.append('') - self._htColorTable = ''.join(colorTable) - return self._htColorTable + %s + + ''' % (self._color, self._bgcolor, colorTable)) Modified: Webware/trunk/WebKit/Examples/FileUpload.py ============================================================================== --- Webware/trunk/WebKit/Examples/FileUpload.py (original) +++ Webware/trunk/WebKit/Examples/FileUpload.py Tue Aug 22 17:44:13 2006 @@ -27,7 +27,7 @@ f = self.request().field('filename') contents = f.file.read() except: - output = '''

%s

+ output = '''

%s

```

 [Webware-checkins] r5631 - Webware/trunk/WebKit/Examples From: - 2006-08-22 23:44:20 ```Author: chrisz Date: Tue Aug 22 17:44:13 2006 New Revision: 5631 Modified: Webware/trunk/WebKit/Examples/Colors.py Webware/trunk/WebKit/Examples/FileUpload.py Log: More cosmetic on Examples Modified: Webware/trunk/WebKit/Examples/Colors.py ============================================================================== --- Webware/trunk/WebKit/Examples/Colors.py (original) +++ Webware/trunk/WebKit/Examples/Colors.py Tue Aug 22 17:44:13 2006 @@ -1,75 +1,90 @@ import os from ExamplePage import ExamplePage + +# Helper functions + +gamma = 2.2 # an approximation for today's CRTs + +def brightness(r, g, b): + """Calculate brightness of RGB color.""" + r, g, b = map(lambda x: x/255.0, (r, g, b)) + return (0.3*r**gamma + 0.6*g**gamma + 0.1*b**gamma)**(1/gamma) + +def textcolor(r, g, b): + """Determine a good text font color for high contrast.""" + return brightness(r, g, b) < 0.5 and 'white' or 'black' + +def RGBToHTMLColor(r, g, b): + """Convert r, g, b to #RRGGBB.""" + return '#%02X%02X%02X' % (r, g, b) + +def HTMLColorToRGB(h): + """Convert #RRGGBB to r, g, b.""" + h = h.strip() + if h.startswith('#'): + h = h[1:] + h = h[:2], h[2:4], h[4:] + return map(lambda x: int(x, 16), h) + +# Prepare HTML for color table + +numSteps = 6 # this gives the "web-safe" color palette +steps = map(lambda x: 255.0*x/(numSteps-1), range(numSteps)) + +colorTable = [ + '

Click on one of the colors below to set the background color.

', + ''] +for r in steps: + for g in steps: + colorTable.append('
%s
\n') + for b in steps: + color = RGBToHTMLColor(r, g, b) + colorTable.append('\n' + % (color, textcolor(r, g, b), color, color)) + colorTable.append('\n') +colorTable.append('') +colorTable = ''.join(colorTable) + + class Colors(ExamplePage): """Colors demo. This class is a good example of caching. The color table that - this servlet creates never changes, so the servlet caches this in - the _htColorTable attribute. The original version of this + this servlet creates never changes, so the servlet caches this + in the global colorTable variable. The original version of this example did no caching and was 12 times slower. """ - def __init__(self): - ExamplePage.__init__(self) - self._htColorTable = None - - def awake(self, trans): - """Set _bgcolor according to our field.""" - ExamplePage.awake(self, trans) - self._bgcolor = '' - self._bgcolorArg = '' - req = self.request() - if req.hasField('bgcolor'): - self._bgcolor = req.field('bgcolor').strip() - def htBodyArgs(self): - """Overridden to throw in the custom background color that the user can specify in our form.""" - bgcolor = self._bgcolor or 'white' - return 'color="black" bgcolor="%s" ' \ - 'style="color:black;background-color:%s"' % ((bgcolor,)*2) + """Write the attributes of the body element. + + Overridden in order to throw in the custom background color + that the user can specify in our form. + + """ + self._bgcolor = self.request().field('bgcolor', '#FFFFFF') + try: + r, g, b = HTMLColorToRGB(self._bgcolor) + self._color = textcolor(r, g, b) + except: + self._color = 'black' + return 'text="black" bgcolor="%s" style="background-color:%s"' \ + % ((self._bgcolor,)*2) def writeContent(self): - self.writeln('
') + """Write the actual content of the page.""" self.write(''' +

Color Table Demo

- Background color: + Background color: - ''' % (self._bgcolor)) - self.writeln(self.htColorTable()) - self.writeln('
') - - def htColorTable(self): - if self._htColorTable is None: - colorTable = [''] - gamma = 2.2 # an approximation for today's CRTs, see "brightness =" below - numSteps = 8 - steps = map(float, range(numSteps)) - denominator = float(numSteps-1) - for r in steps: - r = r/denominator - for g in steps: - g = g/denominator - colorTable.append('
%s
\n') - for b in steps: - b = b/denominator - color = '#%02x%02x%02x' % (r*255, g*255, b*255) - # Compute brightness given RGB: - brightness = (0.3*r**gamma + 0.6*g**gamma + 0.1*b**gamma)**(1/gamma) - # We then use brightness to determine a good font color for high contrast: - if brightness < 0.5: - textcolor = 'white' - else: - textcolor = 'black' - colorTable.append('\n' - % (color, textcolor, color, color)) - colorTable.append('\n') - colorTable.append('') - self._htColorTable = ''.join(colorTable) - return self._htColorTable + %s + + ''' % (self._color, self._bgcolor, colorTable)) Modified: Webware/trunk/WebKit/Examples/FileUpload.py ============================================================================== --- Webware/trunk/WebKit/Examples/FileUpload.py (original) +++ Webware/trunk/WebKit/Examples/FileUpload.py Tue Aug 22 17:44:13 2006 @@ -27,7 +27,7 @@ f = self.request().field('filename') contents = f.file.read() except: - output = '''

%s

+ output = '''

%s

```