diff options
| author | pepperpepperpepper <pepper@scannerjammer.com> | 2015-12-05 16:57:53 -0800 |
|---|---|---|
| committer | pepperpepperpepper <pepper@scannerjammer.com> | 2015-12-05 16:57:53 -0800 |
| commit | e85b0885ac45e5773a2e9dfb2b91bc8a57de98e2 (patch) | |
| tree | d4100b771fa1fd14dd09d62770ae5165bd5fdf4d | |
| parent | 0b0ac03c2f74996b178282bbaa0684229fd18393 (diff) | |
added normalization to params
| -rwxr-xr-x | example.py | 4 | ||||
| -rw-r--r-- | ricky/param/__init__.py | 6 | ||||
| -rw-r--r-- | ricky/param/bool.py | 7 | ||||
| -rw-r--r-- | ricky/param/color.py | 28 | ||||
| -rw-r--r-- | ricky/param/constrainednumber.py | 12 | ||||
| -rw-r--r-- | ricky/param/enum.py | 13 | ||||
| -rw-r--r-- | ricky/params.py | 2 |
7 files changed, 68 insertions, 4 deletions
@@ -6,7 +6,9 @@ api = ImGradient() params = api.params_init() print params params.randomize() -print params +print params['color1'] +params['color1'].from_normalized(0.28187431585) +print params['color1'] # print params.as_dict() # req = params.execute() # print req diff --git a/ricky/param/__init__.py b/ricky/param/__init__.py index 70dff65..cbacf6f 100644 --- a/ricky/param/__init__.py +++ b/ricky/param/__init__.py @@ -55,3 +55,9 @@ class Param(object): def randomize(self): pass + + def from_normalized(self, value): + pass + + def as_normalized(self): + return 0 diff --git a/ricky/param/bool.py b/ricky/param/bool.py index 961912e..2b25bbd 100644 --- a/ricky/param/bool.py +++ b/ricky/param/bool.py @@ -34,3 +34,10 @@ class Bool(Param): def randomize(self): self.value_set(random.choice([True, False])) + + def from_normalized(self, value): + value_as_int = int(round(value, 0)) + self.value = True if value_as_int else False + + def as_normalized(self): + return 1 if self.value else 0 diff --git a/ricky/param/color.py b/ricky/param/color.py index 2654914..5da05ed 100644 --- a/ricky/param/color.py +++ b/ricky/param/color.py @@ -1,5 +1,8 @@ from ricky.param import Param import random +import math +import decimal +import re class Color(Param): @@ -8,7 +11,29 @@ class Color(Param): @classmethod def from_rgb(cls, r, g, b): - return cls(value="rgb({},{},{})".format(r, g, b)) + return cls(value="rgb({},{},{})".format(r, g, b), is_rgb=True) + + def as_hex(self): + matched = re.match( + r'rgb\(([0-9]+),([0-9]+),([0-9]+)\)', + self.value, + re.IGNORECASE + ) + return ''.join( + [hex(int(num)).split('x')[1] for num in matched.groups()] + ) + + def from_normalized(self, value): + maximum = math.pow(16, 6) - 1 + hex_val = hex(int(round(maximum * value, 0))) + matched = re.match('0x(..)(..)(..)', hex_val) + vals = [int(num, 16) for num in matched.groups()] + self.value = "rgb({},{},{})".format(vals[0], vals[1], vals[2]) + + def as_normalized(self): + maximum = math.pow(16, 6) - 1 + value = int(self.as_hex(), 16) + return decimal.Decimal(value)/decimal.Decimal(maximum) def randomize(self): self.value = "rgb(%s,%s,%s)" % ( @@ -16,3 +41,4 @@ class Color(Param): random.randint(0, 255), random.randint(0, 255) ) + self._is_rgb = True diff --git a/ricky/param/constrainednumber.py b/ricky/param/constrainednumber.py index 30c7a45..3cf009e 100644 --- a/ricky/param/constrainednumber.py +++ b/ricky/param/constrainednumber.py @@ -1,4 +1,5 @@ import random +import decimal from ricky.param import Param DEFAULT_RAND_MAX = 1000 DEFAULT_RAND_MIN = -1000 @@ -89,3 +90,14 @@ class ConstrainedNumber(Param): raise ValueError( "Unable to set random value on %s in %s tries" ) % (self.name, self.tries_max) + + def from_normalized(self, value): + total_range = self.range_max - self.range_min + val_in_range = hex(int(round(total_range * value, 0))) + self.value = val_in_range + self.range_min + + def as_normalized(self): + total_range = self.range_max - self.range_min + return decimal.Decimal( + self.value + self.range_min + )/decimal.Decimal(total_range) diff --git a/ricky/param/enum.py b/ricky/param/enum.py index f0adc9e..3a9fe0e 100644 --- a/ricky/param/enum.py +++ b/ricky/param/enum.py @@ -1,10 +1,11 @@ import random from ricky.param import Param +import decimal class Enum(Param): def __init__(self, options=None, **kwargs): - self._options = options + self._options = set(options) if not self._options: raise ValueError("Object must be initialized with options set") super(Enum, self).__init__(**kwargs) @@ -36,3 +37,13 @@ class Enum(Param): my_dict = super(Enum, self).as_dict() my_dict['options'] = self._options return my_dict + + def from_normalized(self, value): + maximum = len(self._options - 1) + idx_val = int(round(maximum * value, 0)) + self.value = self._options[idx_val] + + def as_normalized(self): + maximum = len(self._options - 1) + value = int(self.as_hex(), 16) + return decimal.Decimal(value)/decimal.Decimal(maximum) diff --git a/ricky/params.py b/ricky/params.py index 3d5677b..c5595d4 100644 --- a/ricky/params.py +++ b/ricky/params.py @@ -7,7 +7,7 @@ class Params(object): self._api = None self._params = args - def param(self, name): + def __getitem__(self, name): """getter for the param by name""" for param in self._params: if param.name == name: |
