summaryrefslogtreecommitdiff
path: root/Pb_Api/Param/Color.py
blob: 88bd9bdab45709b7d931b02d717f225f1e7bb491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from Pb_Api.Param.MultiSelect import Pb_Api_Param_MultiSelect
import random
class Pb_Api_Param_Color(Pb_Api_Param_MultiSelect): 
    def __init__(self, **kwargs):
      super(Pb_Api_Param_Color, self).__init__(**kwargs)  

    @classmethod
    def from_rgb(cls, r,g,b):
      return cls(value="rgb({},{},{})".format(r,g,b))
    
    def randomize(self):
       weights_total = sum(map(lambda x: x["weight"], self.options()) +  (255 * 255 * 255) 
       choice = random.randint(0, weights_total)
       position = 0
       for elem in self.options():
          position += elem["weight"] 
          if position >= choice:
              self.value = elem["value"]
              return
       self.value = "rgb({},{},{})".format(
         random.randint(0,255),
         random.randint(0,255),
         random.randint(0,255),
       )