[pypy-svn] r71418 - in pypy/trunk/pypy/jit/tool: . test

arigo at codespeak.net arigo at codespeak.net
Mon Feb 22 18:02:25 CET 2010


Author: arigo
Date: Mon Feb 22 18:02:22 2010
New Revision: 71418

Modified:
   pypy/trunk/pypy/jit/tool/otherviewer.py
   pypy/trunk/pypy/jit/tool/test/test_otherviewer.py
Log:
Playing with colors: instead of just red-or-green-or-yellow,
have a gradiant of all colors, based on the ratio.


Modified: pypy/trunk/pypy/jit/tool/otherviewer.py
==============================================================================
--- pypy/trunk/pypy/jit/tool/otherviewer.py	(original)
+++ pypy/trunk/pypy/jit/tool/otherviewer.py	Mon Feb 22 18:02:22 2010
@@ -5,6 +5,7 @@
 import optparse
 import sys
 import re
+import math
 
 import autopath
 from pypy.translator.tool.graphpage import GraphPage
@@ -30,16 +31,26 @@
     def name(self):
         return 'node' + str(self.no)
 
-    def getcolor(self):
-        if self.ratio > 8:
-            return 'red'
-        elif self.ratio > 5:
-            return 'yellow'
-        return 'green'
-
     def generate(self, dotgen, memo):
         dotgen.emit_node(self.name(), label=self.content,
-                         shape='box', fillcolor=self.getcolor())
+                         shape='box', fillcolor=get_gradient_color(self.ratio))
+
+def get_gradient_color(ratio):
+    ratio = math.log(ratio)      # from -infinity to +infinity
+    #
+    # ratio: <---------------------- 1.8 --------------------->
+    #        <-- towards green ---- YELLOW ---- towards red -->
+    #
+    ratio -= 1.8
+    ratio = math.atan(ratio * 5) / (math.pi/2)
+    # now ratio is between -1 and 1
+    if ratio >= 0.0:
+        # from yellow (ratio=0) to red (ratio=1)
+        return '#FF%02X00' % (int((1.0-ratio)*255.5),)
+    else:
+        # from yellow (ratio=0) to green (ratio=-1)
+        return '#%02XFF00' % (int((1.0+ratio)*255.5),)
+
 
 class FinalBlock(BasicBlock):
     def __init__(self, content, target):

Modified: pypy/trunk/pypy/jit/tool/test/test_otherviewer.py
==============================================================================
--- pypy/trunk/pypy/jit/tool/test/test_otherviewer.py	(original)
+++ pypy/trunk/pypy/jit/tool/test/test_otherviewer.py	Mon Feb 22 18:02:22 2010
@@ -1,7 +1,16 @@
-
+import math
 import py
 from pypy.jit.tool.otherviewer import splitloops, FinalBlock, Block,\
-     split_one_loop, postprocess, main
+     split_one_loop, postprocess, main, get_gradient_color
+
+
+def test_gradient_color():
+    assert get_gradient_color(0.0000000001) == '#01FF00'   # green
+    assert get_gradient_color(100000000000) == '#FF0100'   # red
+    assert get_gradient_color(math.exp(1.8)) == '#FFFF00'  # yellow
+    assert get_gradient_color(math.exp(1.9)) == '#FFB400'  # yellow-a-bit-red
+    assert get_gradient_color(math.exp(1.7)) == '#B4FF00'  # yellow-a-bit-green
+
 
 def preparse(data):
     return "\n".join([i.strip() for i in data.split("\n") if i.strip()])



More information about the Pypy-commit mailing list