#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import cffi
import numpy as np
import matplotlib.pyplot as plt
c_header = '''
void   test (void);
void   print_table  (void);
double ComputeRed   (double);
double ComputeGreen (double);
double ComputeBlue  (double);
'''
def plot_colors(C):
  yr = []; yg = []; yb = [];
  xs = np.arange(0.0, 256.0, 1.0)
  for x in xs: yr.append (C.ComputeRed  (x))
  for x in xs: yg.append (C.ComputeGreen(x))
  for x in xs: yb.append (C.ComputeBlue (x))
  fig,ap = plt.subplots (1, figsize=(6.0,4.0))
  fig.suptitle ('Color functions', fontsize=15)
  ap.plot (xs, yr, 'r-')
  ap.plot (xs, yg, 'g-')
  ap.plot (xs, yb, 'b-')
  ap.legend (['R','G','B'])
  plt.grid()
  plt.ylabel('Intenzity')
  plt.xlabel('N')
  plt.savefig('color.png')
  #plt.show()
  
############################  MAIN  ##############################################################
def main_func():
  ffi = cffi.FFI()
  ffi.cdef(c_header)
  C = ffi.dlopen("./graph.so")
  C.test()
  plot_colors(C)
  C.print_table()
  ffi.dlclose(C)
  return True
if __name__ == '__main__':
  r = main_func()
  print ('END {:s}'.format(str(r)))