def pictaculous_palette(fname): """ Extract a color palette using the pictaculous API. """ endpoint = 'http://pictaculous.com/api/1.0/' r = requests.post(endpoint, {'image': open(fname, 'rb').read()}) data = json.loads(r.text) maincolors = data['info']['colors'] maincolors = np.asarray([hex2rgb(c) for c in maincolors]) / 255. return Palette(maincolors, [1.0 / len(maincolors)] * len(maincolors))
def __init__(self, width=5, color="#FFFFFF"): self.x = 100 self.y = 0 self.x_step = 5 self.y_step = 0 self.history = [] self.tail = 0 self.color = hex2rgb(color) self.width = width
def generate_new_theme(): """Retrieve new theme from colormind, store in state, and apply to app.""" if any(locked): # Generate only new colors for the colors that are not locked. These need to be # represented as "N" in the list below. Locked colors need to be represented by # their RGB values, e.g. [123, 123, 123]. input_list = ["N", "N", "N", "N", "N"] # TODO: Refactor this. if locked[0]: if st.session_state.is_dark_theme: input_list[4] = utils.hex2rgb(st.session_state.backgroundColor) else: input_list[0] = utils.hex2rgb(st.session_state.backgroundColor) if locked[1]: if st.session_state.is_dark_theme: input_list[3] = utils.hex2rgb( st.session_state.secondaryBackgroundColor) else: input_list[1] = utils.hex2rgb( st.session_state.secondaryBackgroundColor) if locked[2]: input_list[2] = utils.hex2rgb(st.session_state.primaryColor) if locked[3]: if st.session_state.is_dark_theme: input_list[0] = utils.hex2rgb(st.session_state.textColor) else: input_list[4] = utils.hex2rgb(st.session_state.textColor) res = requests.get("http://colormind.io/api/", json={ "input": input_list, "model": "ui" }) else: # Generate new colors for all colors. res = requests.get("http://colormind.io/api/", json={"model": "ui"}) # Retrieve results from colormind.io and convert to hex. rgb_colors = res.json()["result"] hex_colors = [utils.rgb2hex(*rgb) for rgb in res.json()["result"]] # TODO: Refactor this with the stuff above. # Store colors in session state. This is required so that separate tabs/users can # have different themes. If we would apply the theme directly to `st.config`, # every user would see the same theme! if theme_type == "Light theme": st.session_state.primaryColor = hex_colors[2] st.session_state.backgroundColor = hex_colors[0] st.session_state.secondaryBackgroundColor = hex_colors[1] st.session_state.textColor = hex_colors[4] st.session_state.is_dark_theme = False else: st.session_state.primaryColor = hex_colors[2] st.session_state.backgroundColor = hex_colors[4] st.session_state.secondaryBackgroundColor = hex_colors[3] st.session_state.textColor = hex_colors[0] st.session_state.is_dark_theme = True
def fillRGB(self, kwargs): """ kwargs - color - hex color code, i.g. '#4466FF' - start - zero-based starting index -end - zero-ending index """ r, g, b = hex2rgb(kwargs["color"]) start = kwargs.get("start", 0) end = kwargs.get("end", self.NLEDS) self.led.fillRGB(r, g, b, start, end) self.led.update()
def get_text_color(self, message): textColor = graphics.Color(100, 20, 255) if 'color' in message: try: color_tuple = hex2rgb(message['color']) textColor = graphics.Color(color_tuple[0], color_tuple[1], color_tuple[2]) except: pass return textColor
def get_text_color(self, message): textColor = graphics.Color(0, 255, 0) if 'color' in message: try: color_tuple = hex2rgb(message['color']) textColor = graphics.Color(color_tuple[0], color_tuple[1], color_tuple[2]) except: print("problem with color lookup") print("-" * 60) traceback.print_exc(file=sys.stdout) print("-" * 60) pass return textColor
def run(self): dt = 0.1 print "Starting thread %s" % (self.name) offset = 0 while not self.stoprequest.isSet(): for i in range(0, self.ctenophore.NLEDS): val = (i + offset) % self.ctenophore.NLEDS if i % 3 == 0: color = "#AA0000" elif i % 3 == 1: color = "#00AA00" elif i % 3 == 2: color = "#AAAAAA" r, g, b = hex2rgb(color) self.ctenophore.led.setRGB(val, r, g, b) offset = (offset + 1) % 3 self.ctenophore.led.update() time.sleep(0.1) if self.stoprequest.isSet(): print "******** STOP IT ************ %s" % self.name
# Add parent vis_path = '/root/mount/oc-fewshot/sim/visualization' os.chdir(vis_path) import utils os.chdir(parent_dir) # Add parent modules_path = '/root/mount/oc-fewshot/sim/' os.chdir(modules_path) import modules.utils import modules.vis_utils import config.vis_model as config_vis import config.color_palette os.chdir(parent_dir) _NEW_BBOX_COLOR = utils.hex2rgb(config.color_palette.colors['gray'], reverse=True) _SEEN_BBOX_COLOR = utils.hex2rgb(config.color_palette.colors['red'], reverse=True) _UNLABELED_IDX = 40 _UNLABELED_KEY = 'Unknown' # _ZOOM_ON = True _SLOW_FACTOR = 4 # 4 _SLOW_FRAC = 1 / 4 def build_instance_lookup(y_full, batch, valid_idxs): y_full = y_full[valid_idxs] instance_ids = batch['instance_id'][0].numpy()[valid_idxs] lookup = {int(key): int(val) for key, val in zip(y_full, instance_ids)} return lookup
def set_state(self, kwargs): """ Accepts a dict called kwargs Starts threads based on the kwargs['state']. kwargs: - state [String] String, it must start with '#'. Possible states are '#off', '#random', '#red-white-blue', '#green-purple' '#glow-warm', '#<HEXCOLOR>', """ state = kwargs['state'] print "[MagicMushroom.set_state()] with state", state print "[set_state] Turning everything off" self.allOff({}) if self.currentThread: self.currentThread.stoprequest.set() for thread in self.blinkThreads: print "[set_state] setting stoprequest on %s" % thread thread.stoprequest.set() self.blinkThreads = [] if state == '#off': return elif state == '#random': for i in range(0, 20): output_queue = Queue.Queue() thread = BlinkThread("Blink Thread %s" %i, self, output_queue, self.NLEDS) thread.start() self.blinkThreads.append(thread) elif state == '#red-white-blue': for i in range(0, self.NLEDS): if i % 3 == 0: color = "#FF0000" elif i % 3 == 1: color = "#FFFFFF" elif i % 3 == 2: color = "#0000FF" r, g, b = hex2rgb(color) self.setRGB({"color": color, "index": i}) self.led.update() elif state == '#xmas': for i in range(0, self.NLEDS): if i % 3 == 0: color = "#AA0000" elif i % 3 == 1: color = "#00AA00" elif i % 3 == 2: color = "#AAAAAA" r, g, b = hex2rgb(color) self.setRGB({"color": color, "index": i}) self.led.update() elif state == '#xmas-motion': output_queue = Queue.Queue() thread = MotionThread("Motion Thread", self, output_queue, self.NLEDS) thread.start() self.currentThread = thread elif state == '#xmas-fade': output_queue = Queue.Queue() thread = XmasFadeThread("Xmas Fade Thread", self, output_queue, self.NLEDS) thread.start() self.currentThread = thread elif state == '#green-purple': for i in range(0, self.NLEDS): if i % 2 == 0: color = "#00FF00" elif i % 2 == 1: color = "#FF00FF" r, g, b = hex2rgb(color) self.setRGB({"color": color, "index": i}) self.led.update() elif state == '#glow-warm': self.led.fillRGB(r, g, b, self.STOCK_HEIGHT+1, self.NLEDS) self.led.update() else: # Assume is a hex-color print "Looking for color %s" % state r, g, b = hex2rgb(state) self.led.fillRGB(r, g, b, self.STOCK_HEIGHT+1, self.NLEDS) self.led.update() self.state = state self.last_state_change = dt.now()
def setRGB(self, kwargs): r, g, b = hex2rgb(kwargs["color"]) self.led.setRGB(kwargs["index"], r, g, b) self.led.update()
def get_color(self, idx): primary, secondary = self.get_color_raw(self.raw_index(idx)) return [*hex2rgb(primary), *hex2rgb(secondary)]
def get_county_plot_by_region(data, colorscale, node_elements, fips): '''Function to create figure of US with counties colored by region they belong to Parameters ---------- data : ndarray (n_samples x n_dim) Data used for mapper colorscale : list List with colors to color counties by node_elements : tuple Tuple of arrays where array at positin x contains the data points for node x fips : list List of Federal Information Processing Standard (FIPS) county codes Returns ------- fig: plotly figure object ''' # convert colorscale from hex format to rgb colorscale = dict(zip(map(str, range(len(colorscale))), utils.hex2rgb(colorscale.values()))) # define color of counties belonging to two regions as their mean rgb value colorscale['1-3'] = utils.mean_rgb([colorscale['1'], colorscale['3']]) colorscale['2-3'] = utils.mean_rgb([colorscale['2'], colorscale['3']]) colorscale['3-4'] = utils.mean_rgb([colorscale['3'], colorscale['4']]) colorscale['4-5'] = utils.mean_rgb([colorscale['4'], colorscale['5']]) elements_per_region = utils.get_data_per_region(utils.get_regions(), node_elements) # assign each county its color county_color = np.zeros(data.shape[0], dtype='int') # region 0 county_color[list(elements_per_region[0])] = 0 # region 1 and not 3 county_color[list(elements_per_region[1] .difference(elements_per_region[3]))] = 1 # region 2 and not 3 county_color[list(elements_per_region[2] .difference(elements_per_region[3]))] = 2 # region 3 and neither 1 nor 2 county_color[list(elements_per_region[3] .difference(elements_per_region[1]) .difference(elements_per_region[2]))] = 3 # region 4 but not 3 county_color[list(elements_per_region[4] .difference(elements_per_region[3]))] = 4 # region 5 but not 4 county_color[list(elements_per_region[5] .difference(elements_per_region[4]))] = 5 # region 1 and 3 county_color[list(elements_per_region[1] .intersection(elements_per_region[3]))] = 6 # region 2 and 3 county_color[list(elements_per_region[2] .intersection(elements_per_region[3]))] = 7 # region 3 and 4 county_color[list(elements_per_region[3] .intersection(elements_per_region[4]))] = 8 # region 4 and 5 county_color[list(elements_per_region[4] .intersection(elements_per_region[5]))] = 9 county_color = county_color.tolist() return get_county_plot( fips=fips, values=county_color, colorscale=[f'rgb{rgb}' for rgb in list(colorscale.values())])
from snake import Snake import pygame from utils import hex2rgb from random import randint from time import sleep from utils import Vector size = width, height = 500, 500 color = hex2rgb("#292828") frameRate = 0.1 if __name__ == "__main__": pygame.init() screen = pygame.display.set_mode(size) screen.fill(color) snake = Snake(width=10) food = Vector(randint(snake.width, width), randint(snake.width, height)) while True: screen.fill((25,25,25)) snake.move() snake.show(screen) end = snake.dead() if snake.eat(food): food = Vector(randint(snake.width, width), randint(snake.width, height)) rect = pygame.Rect(food.x, food.y, snake.width, snake.width) pygame.draw.rect(screen, (255, 0, 0), rect)