import math
import matplotlib.pyplot as plt
import numpy as np

colordict = {
    'orange_alt': (1.0, 0.4980392156862745, 0.054901960784313725),
    'red_alt': (0.8392156862745098, 0.15294117647058825, 0.1568627450980392),
    'green_alt': (0.17254901960784313, 0.6274509803921569, 0.17254901960784313),
    'blue_alt': (0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
    'purple': '#9467bd',
    'brown': '#8c564b',
    'magenta': '#e377c2',
    'lime': '#bcbd22',
    'cyan': '#17becf',
    'ochre':(0.854901961, 0.647058824, 0.125490196),
    'red_grey': (0.901960784,0.784313725,0.784313725),
    'blue_grey': (0.784313725,0.784313725,0.901960784),
    'grey': '#7f7f7f',
    'black': '#181818',
    'white': '#ffffff',
    'blue': '#1f77b4',
    'orange': '#ff7f0e',
    'green': '#2ca02c',
    'yellow':(1,1,0),
    'red': '#d62728',
    'gray': '#7f7f7f',
    'darkblue': '#00008b',
    'darkred': (162./255.,39./255.,48./255),
    'lightblue': '#aec7e8',
    'lightorange': '#ffbb78',
    'lightgreen': '#98df8a',
    'lightred': '#ff9896',
    'lightpurple': '#c5b0d5',
    'lightbrown': '#c49c94',
    'lightmagenta': '#f7b6d2',
    'lightgray': '#c7c7c7',
    'lightgrey': '#c7c7c7',
    'lightlime': '#dbdb8d',
    'lightcyan': '#9edae5',
    'lighterpurple': '#ccccff'
}

linestyledict = {'loosely_dotted': (0, (1, 10)),
    'dotted': (0, (1, 1)), 
    'solid': 'solid', 
    'densely_dotted':(0, (1, 1)),
    'loosely_dashed':(0, (5, 10)),
    'dashed':        (0, (5, 5)),
    'densely_dashed':(0, (5, 1)),
    'loosely_dashdotted': (0, (3, 10, 1, 10)),
    'dashdotted':    (0, (3, 5, 1, 5)),
    'densely_dashdotted':    (0, (3, 1, 1, 1)),
    'dashdotdotted': (0, (3, 5, 1, 5, 1, 5)),
    'loosely_dashdotdotted':(0, (3, 10, 1, 10, 1, 10)),
    'densely_dashdotdotted':(0, (3, 1, 1, 1, 1, 1))}


def plt_rcParams_sci_adv():
    plt.rcParams.update({
    'font.size': 9,
    'axes.labelsize': 9,
    'legend.fontsize': 7,
    'xtick.labelsize': 7,
    'ytick.labelsize': 7,
    'lines.markeredgewidth': 0,
    'legend.framealpha': 1.,
    'legend.frameon': False,
    'lines.markersize': 1,
    'lines.linewidth': 1.,
    'xtick.direction': 'in',
    'ytick.direction': 'in',
    'xtick.minor.size': 2,
    'ytick.minor.size': 2,
    'xtick.major.size': 3,
    'ytick.major.size': 3,
    'xtick.minor.width': .5,
    'xtick.major.width': .5,
    'ytick.minor.width': .5,
    'ytick.major.width': .5,
    'axes.linewidth': .5,
    'axes.labelpad': 0.5,
    'text.usetex': True,
    'text.latex.preview' : False,
    'font.sans-serif': 'xkcd',
    'font.serif': 'Computer Modern Roman',
    'font.monospace': 'Computer Modern Typewriter',
    'text.latex.preamble': r'\usepackage{amsmath,amssymb}\usepackage{sansmath}\sansmath',
    'hatch.color': 'lightgrey',
    'hatch.linewidth': 1.,

    })


def get_equal_gray_scale(arr):
    size = arr.shape[0] - 1
    color_list = [(0.05 + 0.85*i/size,0.05 + 0.85*i/size,0.05 + 0.85*i/size) for i in range(size+1)]
    return np.array(color_list)
    

def gini(x):
    sorted_x = np.sort(x)
    n = len(x)
    cumx = np.cumsum(sorted_x, dtype=float)
    return (n + 1 - 2 * np.sum(cumx) / cumx[-1]) / n
    
def point_to_p(number,digits=1):
    return f"{number:.{digits}f}".replace('.','p')
    
