Advertisement
patryk

KCK - Zad 2 8%

Oct 20th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import division             # Division in Python 2.7
  4. import matplotlib
  5. matplotlib.use('Agg')                       # So that we can render files without GUI
  6. import matplotlib.pyplot as plt
  7. from matplotlib import rc
  8. import numpy as np
  9. from matplotlib import colors
  10. import math as math
  11.  
  12. def plot_color_gradients(gradients, names):
  13.     rc('legend', fontsize=10)
  14.  
  15.     column_width_pt = 400         # Show in latex using \the\linewidth
  16.     pt_per_inch = 72
  17.    
  18.     size = column_width_pt / pt_per_inch
  19.  
  20.     fig, axes = plt.subplots(nrows=len(gradients), sharex=True, figsize=(size, 0.75 * size))
  21.     fig.subplots_adjust(top=1.00, bottom=0.05, left=0.25, right=0.95)
  22.  
  23.  
  24.     for ax, gradient, name in zip(axes, gradients, names):
  25.         img = np.zeros((2, 1024, 3))
  26.    
  27.         for i, v in enumerate(np.linspace(0, 1, 1024)):
  28.             img[:, i] = gradient(v)
  29.    
  30.  
  31.         im = ax.imshow(img, aspect='auto')
  32.         im.set_extent([0, 1, 0, 1])
  33.         ax.yaxis.set_visible(False)
  34.  
  35.         pos = list(ax.get_position().bounds)
  36.         x_text = pos[0] - 0.25
  37.         y_text = pos[1] + pos[3]/2.
  38.         fig.text(x_text, y_text, name, va='center', ha='left', fontsize=10)
  39.  
  40.         fig.savefig('my-gradients.pdf')
  41.  
  42. def Interpolate2Colors(A, B, pos, max_pos):
  43.     A_R = A >> 16
  44.     A_G = (A >> 8) & 0xff
  45.     A_B = A & 0xff
  46.  
  47.     B_R = B >> 16
  48.     B_G = (B >> 8) & 0xff
  49.     B_B = B & 0xff
  50.  
  51.     C_R = (((B_R - A_R) * pos) / max_pos + A_R) / 255
  52.     C_G = (((B_G - A_G) * pos) / max_pos + A_G) / 255
  53.     C_B = (((B_B - A_B) * pos) / max_pos + A_B) / 255
  54.  
  55.     return (C_R, C_G, C_B)
  56.  
  57.  
  58.  
  59. def InterpolateNColors(c_list, value, max_value):
  60.     for i in range(1, len(c_list)):
  61.         if (value < i * max_value / (len(c_list)-1)):
  62.             color = Interpolate2Colors(c_list[i-1], c_list[i], value - (((i-1)*max_value) / (len(c_list)-1)), max_value / (len(c_list)-1))         
  63.             return color
  64.  
  65.  
  66.  
  67.  
  68. def hsv2rgb(h, s, v):
  69.     if s == 0.0: v*=255; return [v, v, v]
  70.     i = int(h*6.)
  71.     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
  72.     if i == 0: return '0x%02x%02x%02x' % (v, t, p)
  73.     if i == 1: return '0x%02x%02x%02x' % (q, v, p)
  74.     if i == 2: return '0x%02x%02x%02x' % (p, v, t)
  75.     if i == 3: return '0x%02x%02x%02x' % (p, q, v)
  76.     if i == 4: return '0x%02x%02x%02x' % (t, p, v)
  77.     if i == 5: return '0x%02x%02x%02x' % (v, p, q)
  78.  
  79. def gradient_rgb_bw(v):
  80.     return (v, v, v)
  81.  
  82.  
  83. def gradient_rgb_gbr(v):
  84.     return InterpolateNColors([0x00FF00, 0x0000FF, 0xFF0000], v, 1)
  85.  
  86.  
  87. def gradient_rgb_gbr_full(v):
  88.     return InterpolateNColors([0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000], v, 1)
  89.  
  90.  
  91. def gradient_rgb_wb_custom(v):
  92.     return InterpolateNColors([0xFFFFFF, 0xFF00FF, 0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0x000000], v, 1)
  93.  
  94.  
  95. def gradient_hsv_bw(v):
  96.     #return InterpolateNColors([int(hsv2rgb(0,0,0), 16), int(hsv2rgb(0,0,1), 16)], v, 1)
  97.     return (v, v, v)
  98.  
  99. def gradient_hsv_gbr(v):
  100.     return InterpolateNColors([0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000], v, 1)
  101.  
  102. def gradient_hsv_unknown(v):
  103.     return InterpolateNColors([0x80FF7F, 0xE5FF7F, 0xFF807F], v, 1)
  104.  
  105.  
  106. def gradient_hsv_custom(v):
  107.     return InterpolateNColors([0xFD02DC, 0x80DC2A,  0xF91A0D, 0xDDCC02, 0xFF0290, 0x00AA23, 0x30FD20], v, 1)
  108.     return InterpolateNColors([0x00FF00, 0xFFFF00, 0xFF00FF, 0x00DDFF], v, 1)  
  109.    
  110.  
  111. if __name__ == '__main__':
  112.     def toname(g):
  113.         return g.__name__.replace('gradient_', '').replace('_', '-').upper()
  114.  
  115.     gradients = (gradient_rgb_bw, gradient_rgb_gbr, gradient_rgb_gbr_full, gradient_rgb_wb_custom,
  116.         gradient_hsv_bw, gradient_hsv_gbr, gradient_hsv_unknown, gradient_hsv_custom)
  117.  
  118.     plot_color_gradients(gradients, [toname(g) for g in gradients])
  119.     print(int(hsv2rgb(240, 1, 1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement