Last active
August 22, 2017 16:32
-
-
Save VitorDiToro/1546c0b99efd07bfb1de1684b4da3102 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sys | |
def rgb_to_cmyk(r,g,b): | |
"""Converte o padrão de cores RGB para o padrão CMYK.""" | |
if r == 0 and g == 0 and b ==0: | |
return 0.0,0.0,0.0,1.0 | |
r_linha = r / 255.0 | |
g_linha = g / 255.0 | |
b_linha = b / 255.0 | |
k = 1.0 - max(r_linha, g_linha, b_linha) | |
c = (1.0 - r_linha - k) / (1.0 - k) | |
m = (1.0 - g_linha - k) / (1.0 - k) | |
y = (1.0 - b_linha - k) / (1.0 - k) | |
#Round to 4 decimal places | |
c = round(c , 4) | |
m = round(m , 4) | |
y = round(y , 4) | |
k = round(k , 4) | |
return c,m,y,k | |
def cmyk_to_rgb(c,m,y,k): | |
"""Converte o padrão de cores CMYK para o padrão RGB.""" | |
r = (1.0 - c) * (1.0 - k) * 255 | |
g = (1.0 - m) * (1.0 - k) * 255 | |
b = (1.0 - y) * (1.0 - k) * 255 | |
r = int(round(r)) | |
g = int(round(g)) | |
b = int(round(b)) | |
return r,g,b | |
def rgb_to_hsv(r,g,b): | |
"""Converte o padrão de cores RGB para o padrão HSV.""" | |
c_max = max(r, g, b) * 1.0 | |
c_min = min(r, g, b) * 1.0 | |
delta = c_max - c_min | |
#Hue Calculation | |
h = 0 if (delta == 0) else\ | |
60 * ((g - b) / delta % 6) if c_max == r else \ | |
60 * ((b - r) / delta + 2) if c_max == g else \ | |
60 * ((r - g) / delta + 4) #if c_max == b | |
#Saturation Calculation | |
s = delta / c_max * 100.0 if c_max != 0 else 0 | |
#Value Calculation | |
v = c_max/2.55 | |
#Round to 2 decimal places | |
h = round(h, 2) | |
s = round(s, 2) | |
v = round(v, 2) | |
return (h,s,v) | |
def hsv_to_rgb(h,s,v): | |
"""Blá Blá Blá""" | |
s = s / 100.0 | |
v = v / 100.0 | |
c = v * s | |
x = c * (1 - abs( (h / 60) % 2 -1 )) | |
m = v - c | |
c = (c + m) * 255 | |
x = (x + m) * 255 | |
t = m * 255 | |
r,g,b = (c,x,t) if (h >= 0 and h < 60 ) else \ | |
(x,c,t) if (h >= 60 and h < 120) else \ | |
(t,c,x) if (h >= 120 and h < 180) else \ | |
(t,x,c) if (h >= 240 and h < 300) else \ | |
(c,t,x) #if (h >= 300 and h< 360) | |
# Raound to next int value | |
r = int(round(r)) | |
g = int(round(g)) | |
b = int(round(b)) | |
return (r,g,b) | |
def hsv_to_cmyk(h,s,v): | |
r,g,b = hsv_to_rgb(h,s,v) | |
return rgb_to_cmyk(r,g,b) | |
def cmyk_to_hsv(c,m,y,k): | |
r,g,b = cmyk_to_rgb(c,m,y,k) | |
return rgb_to_hsv(r,g,b) | |
def presenter(values, type): | |
if type == 'rgb': | |
print('(R)ed = ' + str(values[0])) | |
print('(G)reen = ' + str(values[1])) | |
print('(B)lue = ' + str(values[2])) | |
elif type == 'cmyk': | |
print('(C)yan = ' + str(values[0] * 100) + '%') | |
print('(M)agenta = ' + str(values[1] * 100) + '%') | |
print('(Y)ellow = ' + str(values[2] * 100) + '%') | |
print('(K)ey = ' + str(values[3] * 100) + '%') | |
elif type == 'hsv': | |
print('(H)ue = ' + str(values[0]) + "º") | |
print('(S)aturation = ' + str(values[1]) + "%") | |
print('(V)alue = ' + str(values[2]) + "%") | |
def main(): | |
# Caso o programa seja rodado sem argumentos | |
if len(sys.argv) <= 1: | |
print('Argumentos inválidos. Use -h para obter ajuda.') | |
exit() | |
# Obtem o sistema de cor [Origem] e o [Destino] | |
de = sys.argv[1][1:] | |
para = sys.argv[-1] | |
# Verifica a validade dos argumentos | |
if not ((len(sys.argv) == 6 and de in ['rgb','hsv']) or \ | |
(len(sys.argv) == 7 and de == 'cmyk') \ | |
and para in ['rgb','cmyk','hsv'] and de != para): | |
print('Argumentos inválidos. Use -h para obter ajuda.') | |
exit() | |
# Se tudo OK, executa a conversão para cada sistema de cores | |
if de == 'rgb' and para == 'cmyk': | |
r = int(sys.argv[2]) | |
g = int(sys.argv[3]) | |
b = int(sys.argv[4]) | |
result = rgb_to_cmyk(r,g,b) | |
presenter(result, 'cmyk') | |
elif de == 'cmyk' and para == 'rgb': | |
c = float(sys.argv[2]) | |
m = float(sys.argv[3]) | |
y = float(sys.argv[4]) | |
k = float(sys.argv[5]) | |
result = cmyk_to_rgb(c,m,y,k) | |
presenter(result, 'rgb') | |
elif de == 'rgb' and para == 'hsv': | |
r = int(sys.argv[2]) | |
g = int(sys.argv[3]) | |
b = int(sys.argv[4]) | |
result = rgb_to_hsv(r,g,b) | |
presenter(result, 'hsv') | |
elif de == 'hsv' and para == 'rgb': | |
h = float(sys.argv[2]) | |
s = float(sys.argv[3]) | |
v = float(sys.argv[4]) | |
result = hsv_to_rgb(h,s,v) | |
presenter(result, 'rgb') | |
elif de == 'hsv' and para == 'cmyk': | |
h = float(sys.argv[2]) | |
s = float(sys.argv[3]) | |
v = float(sys.argv[4]) | |
result = hsv_to_cmyk(h,s,v) | |
presenter(result, 'cmyk') | |
elif de == 'cmyk' and para == 'hsv': | |
c = float(sys.argv[2]) | |
m = float(sys.argv[3]) | |
y = float(sys.argv[4]) | |
k = float(sys.argv[5]) | |
result = cmyk_to_hsv(c,m,y,k) | |
presenter(result, 'hsv') | |
elif sys.argv[1] == '-h' or sys.argv[1] == '--help': | |
print('Use: ./<ColocarONomeDoProgramaAqui> -[Sistema Origem] {valores separador por expaço} [Sistema Destino]') | |
print('') | |
print('Sistemas conhecidos:') | |
print('rgb, cmyk e hsv') | |
print('') | |
print('Exemplos:') | |
print('- Para converter uma cor de RGB para CMYK') | |
print('\t./<nome> -rgb 125 85 32 cmyk') | |
print('- Para converter uma cor de CMYK para HSV') | |
print('\t./<nome> -hsv <colocar os valores> cmyk') | |
print('') | |
print('Para mais informações, utilize -man') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment