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):
- 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):
- 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):
- if s == 0.0: v*=255; return [v, v, v]
- i = int(h*6.)
- f = (h*6.)-i; p,q,t = int(255*(v*(1.-s))), int(255*(v*(1.-s*f))), int(255*(v*(1.-s*(1.-f)))); v*=255; i%=6
- if i == 0: return '0x%02x%02x%02x' % (v, t, p)
- if i == 1: return '0x%02x%02x%02x' % (q, v, p)
- if i == 2: return '0x%02x%02x%02x' % (p, v, t)
- if i == 3: return '0x%02x%02x%02x' % (p, q, v)
- if i == 4: return '0x%02x%02x%02x' % (t, p, v)
- if i == 5: return '0x%02x%02x%02x' % (v, p, q)
- 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 InterpolateNColors([int(hsv2rgb(0,0,0), 16), int(hsv2rgb(0,0,1), 16)], v, 1)
- 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, 0xF91A0D, 0xDDCC02, 0xFF0290, 0x00AA23, 0x30FD20], v, 1)
- return InterpolateNColors([0x00FF00, 0xFFFF00, 0xFF00FF, 0x00DDFF], 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(int(hsv2rgb(240, 1, 1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement