Advertisement
patryk

KCK - Zadanie 2 na 3.0

Oct 20th, 2015
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 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.     # For pretty latex fonts (commented out, because it does not work on some machines)
  14.     #rc('text', usetex=True)
  15.     #rc('font', family='serif', serif=['Times'], size=10)
  16.     rc('legend', fontsize=10)
  17.  
  18.     column_width_pt = 400         # Show in latex using \the\linewidth
  19.     pt_per_inch = 72
  20.    
  21.     size = column_width_pt / pt_per_inch
  22.  
  23.     fig, axes = plt.subplots(nrows=len(gradients), sharex=True, figsize=(size, 0.75 * size))
  24.     fig.subplots_adjust(top=1.00, bottom=0.05, left=0.25, right=0.95)
  25.  
  26.  
  27.     for ax, gradient, name in zip(axes, gradients, names):
  28.         # Create image with two lines and draw gradient on it
  29.         img = np.zeros((2, 1024, 3))
  30.    
  31.         for i, v in enumerate(np.linspace(0, 1, 1024)):
  32.             img[:, i] = gradient(v)
  33.    
  34.  
  35.         im = ax.imshow(img, aspect='auto')
  36.         im.set_extent([0, 1, 0, 1])
  37.         ax.yaxis.set_visible(False)
  38.  
  39.         pos = list(ax.get_position().bounds)
  40.         x_text = pos[0] - 0.25
  41.         y_text = pos[1] + pos[3]/2.
  42.         fig.text(x_text, y_text, name, va='center', ha='left', fontsize=10)
  43.  
  44.         fig.savefig('my-gradients.pdf')
  45.  
  46. def Interpolate2Colors(A, B, pos, max_pos):
  47.     A_R = A >> 16
  48.     A_G = (A >> 8) & 0xff
  49.     A_B = A & 0xff
  50.  
  51.     B_R = B >> 16
  52.     B_G = (B >> 8) & 0xff
  53.     B_B = B & 0xff
  54.  
  55.     C_R = (((B_R - A_R) * pos) / max_pos + A_R) / 255
  56.     C_G = (((B_G - A_G) * pos) / max_pos + A_G) / 255
  57.     C_B = (((B_B - A_B) * pos) / max_pos + A_B) / 255
  58.  
  59.     return (C_R, C_G, C_B)
  60.  
  61.  
  62.  
  63. def InterpolateNColors(c_list, value, max_value):
  64.     for i in range(1, len(c_list)):
  65.         if (value < i * max_value / (len(c_list)-1)):
  66.             color = Interpolate2Colors(c_list[i-1], c_list[i], value - (((i-1)*max_value) / (len(c_list)-1)), max_value / (len(c_list)-1))         
  67.             return color
  68.  
  69.  
  70.  
  71. def hsv2rgb(h, s, v):
  72.     RGBcolor = [0,0,0]
  73.     r = 0
  74.     g = 0
  75.     b = 0
  76.  
  77.     if (s==0):
  78.         r = g = b = v;
  79.     else:
  80.         if(h==1):
  81.             h = 0
  82.         z = math.floor(h*6);
  83.         i = int(z);
  84.         f = (h*6 - z);
  85.         p = v*(1-s);
  86.         q = v*(1-s*f);
  87.         t = v*(1-s*(1-f));
  88.         if (i == 0):
  89.             r=v
  90.             g=t
  91.             b=p
  92.         elif (i == 1):
  93.             r=q
  94.             g=v
  95.             b=p
  96.         elif (i == 2):
  97.             r=p
  98.             g=v
  99.             b=t
  100.         elif (i == 3):
  101.             r=p
  102.             g=q
  103.             b=v
  104.         elif (i == 4):
  105.             r=t
  106.             g=p
  107.             b=v
  108.         elif (i == 5):
  109.             r=v
  110.             g=p
  111.             b=q
  112.        
  113.     c = int((256*r))
  114.     if(c>255):
  115.         c = 255
  116.     RGBcolor[0] = c
  117.    
  118.     c = int((256*g))
  119.     if(c>255):
  120.         c = 255
  121.     RGBcolor[1] = c
  122.  
  123.     c = int((256*b))
  124.     if(c>255):
  125.         c = 255
  126.     RGBcolor[2] = c
  127.  
  128.     return (RGBcolor[0], RGBcolor[1], RGBcolor[2])
  129.  
  130. def gradient_rgb_bw(v):
  131.     return (v, v, v)
  132.  
  133.  
  134. def gradient_rgb_gbr(v):
  135.     return InterpolateNColors([0x00FF00, 0x0000FF, 0xFF0000], v, 1)
  136.  
  137.  
  138. def gradient_rgb_gbr_full(v):
  139.     return InterpolateNColors([0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000], v, 1)
  140.  
  141.  
  142. def gradient_rgb_wb_custom(v):
  143.     return InterpolateNColors([0xFFFFFF, 0xFF00FF, 0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0x000000], v, 1)
  144.  
  145.  
  146. def gradient_hsv_bw(v):
  147.     return (v, v, v)
  148.  
  149.  
  150. def gradient_hsv_gbr(v):
  151.     return InterpolateNColors([0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0xFF0000], v, 1)
  152.  
  153. def gradient_hsv_unknown(v):
  154.     return InterpolateNColors([0x80FF7F, 0xE5FF7F, 0xFF807F], v, 1)
  155.  
  156.  
  157. def gradient_hsv_custom(v):
  158.     return InterpolateNColors([0xFD02DC, 0x80DC2A, 0x0321FF, 0xF91A0D, 0xD72A2C, 0xFF0290, 0x0022AA, 0x30FD20], v, 1)
  159.  
  160.  
  161. if __name__ == '__main__':
  162.     def toname(g):
  163.         return g.__name__.replace('gradient_', '').replace('_', '-').upper()
  164.  
  165.     gradients = (gradient_rgb_bw, gradient_rgb_gbr, gradient_rgb_gbr_full, gradient_rgb_wb_custom,
  166.         gradient_hsv_bw, gradient_hsv_gbr, gradient_hsv_unknown, gradient_hsv_custom)
  167.  
  168.     plot_color_gradients(gradients, [toname(g) for g in gradients])
  169.     print(hsv2rgb(120, 100, 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement