Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/elements/tools.py
diff options
context:
space:
mode:
authorWalter Bender <walter@sugarlabs.org>2013-12-11 18:24:21 (GMT)
committer Walter Bender <walter@sugarlabs.org>2013-12-11 18:24:21 (GMT)
commit445dc0cc5770f9aa62264efb37c35d5964e41c0f (patch)
tree995e3ec3d512fe6cc5773e9122b58d3ee9301713 /elements/tools.py
parentfdf37546041d212d97f7fa94dd9013589f9f1f57 (diff)
unpacking elements so as to be able to remove cjson dependency
Diffstat (limited to 'elements/tools.py')
-rw-r--r--elements/tools.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/elements/tools.py b/elements/tools.py
new file mode 100644
index 0000000..9851ff8
--- /dev/null
+++ b/elements/tools.py
@@ -0,0 +1,65 @@
+"""
+This file is part of the 'Elements' Project
+Elements is a 2D Physics API for Python (supporting Box2D2)
+
+Copyright (C) 2008, The Elements Team, <elements@linuxuser.at>
+
+Home: http://elements.linuxuser.at
+IRC: #elements on irc.freenode.org
+
+Code: http://www.assembla.com/wiki/show/elements
+ svn co http://svn2.assembla.com/svn/elements
+
+License: GPLv3 | See LICENSE for the full text
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+# Some Hex Tools
+def hex2dec(hex):
+ """ Convert and hex value in a decimal number
+ """
+ return int(hex, 16)
+
+def hex2rgb(hex):
+ """ Convert a hex color (#123abc) in RGB ((r), (g), (b))
+ """
+ if hex[0:1] == '#': hex = hex[1:];
+ return (hex2dec(hex[:2]), hex2dec(hex[2:4]), hex2dec(hex[4:6]))
+
+def rgb2floats(rgb):
+ """Convert a color in the RGB (0..255,0..255,0..255) format to the
+ (0..1, 0..1, 0..1) float format
+ """
+ ret = []
+ for c in rgb:
+ ret.append(float(c) / 255)
+ return ret
+
+def point_in_poly(point, poly):
+ #print ">", point, poly
+ x, y = point
+ n = len(poly)
+ inside = False
+ p1x,p1y = poly[0]
+ for i in range(n+1):
+ p2x,p2y = poly[i % n]
+ if y > min(p1y,p2y):
+ if y <= max(p1y,p2y):
+ if x <= max(p1x,p2x):
+ if p1y != p2y:
+ xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
+ if p1x == p2x or x <= xinters:
+ inside = not inside
+ p1x,p1y = p2x,p2y
+ return inside
+ \ No newline at end of file