Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from __future__ import division # Division in Python 2.7
- import matplotlib
- matplotlib.use('Agg') # So that we can render files without GUI
- import matplotlib.pyplot as plt
- from matplotlib import rc
- import numpy as np
- from matplotlib import colors
- import math as math
- def plot_color_gradients(gradients, names):
- # For pretty latex fonts (commented out, because it does not work on some machines)
- #rc('text', usetex=True)
- #rc('font', family='serif', serif=['Times'], size=10)
- rc('legend', fontsize=10)
- column_width_pt = 400 # Show in latex using \the\linewidth
- pt_per_inch = 72
- size = column_width_pt / pt_per_inch
- fig, axes = plt.subplots(nrows=len(gradients), sharex=True, figsize=(size, 0.75 * size))
- fig.subplots_adjust(top=1.00, bottom=0.05, left=0.25, right=0.95)
- for ax, gradient, name in zip(axes, gradients, names):
- # Create image with two lines and draw gradient on it
- img = np.zeros((2, 1024, 3))
- for i, v in enumerate(np.linspace(0, 1, 1024)):
- img[:, i] = gradient(v)
- im = ax.imshow(img, aspect='auto')
- im.set_extent([0, 1, 0, 1])
- ax.yaxis.set_visible(False)
- pos = list(ax.get_position().bounds)
- x_text = pos[0] - 0.25
- y_text = pos[1] + pos[3]/2.
- fig.text(x_text, y_text, name, va='center', ha='left', fontsize=10)
- fig.savefig('my-gradients.pdf')
- def Interpolate2Colors(A, B, pos, max_pos):
- A_R = A >> 16
- A_G = (A >> 8) & 0xff
- A_B = A & 0xff
- B_R = B >> 16
- B_G = (B >> 8) & 0xff
- B_B = B & 0xff
- C_R = (((B_R - A_R) * pos) / max_pos + A_R) / 255
- C_G = (((B_G - A_G) * pos) / max_pos + A_G) / 255
- C_B = (((B_B - A_B) * pos) / max_pos + A_B) / 255
- return (C_R, C_G, C_B)
- def InterpolateNColors(c_list, value, max_value):
- for i in range(1, len(c_list)):
- if (value < i * max_value / (len(c_list)-1)):
- color = Interpolate2Colors(c_list[i-1], c_list[i], value - (((i-1)*max_value) / (len(c_list)-1)), max_value / (len(c_list)-1))
- return color
- def hsv2rgb(h, s, v):
- RGBcolor = [0,0,0]
- r = 0
- g = 0
- b = 0
- if (s==0):
- r = g = b = v;
- else:
- if(h==1):
- h = 0
- z = math.floor(h*6);
- i = int(z);
- f = (h*6 - z);
- p = v*(1-s);
- q = v*(1-s*f);
- t = v*(1-s*(1-f));
- if (i == 0):
- r=v
- g=t
- b=p
- elif (i == 1):
- r=q
- g=v
- b=p
- elif (i == 2):
- r=p
- g=v
- b=t
- elif (i == 3):
- r=p
- g=q
- b=v
- elif (i == 4):
- r=t
- g=p
- b=v
- elif (i == 5):
- r=v
- g=p
- b=q
- c = int((256*r))
- if(c>255):
- c = 255
- RGBcolor[0] = c
- c = int((256*g))
- if(c>255):
- c = 255
- RGBcolor[1] = c
- c = int((256*b))
- if(c>255):
- c = 255
- RGBcolor[2] = c
- return (RGBcolor[0], RGBcolor[1], RGBcolor[2])
- def gradient_rgb_bw(v):
- return (v, v, v)
- def gradient_rgb_gbr(v):
- return InterpolateNColors([0x00FF00, 0x0000FF, 0xFF0000], v, 1)
- def gradient_rgb_gbr_full(v):
- return InterpolateNColors([0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000], v, 1)
- def gradient_rgb_wb_custom(v):
- return InterpolateNColors([0xFFFFFF, 0xFF00FF, 0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0x000000], v, 1)
- def gradient_hsv_bw(v):
- return (v, v, v)
- def gradient_hsv_gbr(v):
- return InterpolateNColors([0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000], v, 1)
- def gradient_hsv_unknown(v):
- return InterpolateNColors([0x80FF7F, 0xE5FF7F, 0xFF807F], v, 1)
- def gradient_hsv_custom(v):
- return InterpolateNColors([0xFD02DC, 0x80DC2A, 0x0321FF, 0xF91A0D, 0xD72A2C, 0xFF0290, 0x0022AA, 0x30FD20], v, 1)
- if __name__ == '__main__':
- def toname(g):
- return g.__name__.replace('gradient_', '').replace('_', '-').upper()
- gradients = (gradient_rgb_bw, gradient_rgb_gbr, gradient_rgb_gbr_full, gradient_rgb_wb_custom,
- gradient_hsv_bw, gradient_hsv_gbr, gradient_hsv_unknown, gradient_hsv_custom)
- plot_color_gradients(gradients, [toname(g) for g in gradients])
- print(hsv2rgb(120, 100, 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement