diff options
Diffstat (limited to 'gestion_couleurs.py')
-rw-r--r-- | gestion_couleurs.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/gestion_couleurs.py b/gestion_couleurs.py new file mode 100644 index 0000000..c1b3690 --- /dev/null +++ b/gestion_couleurs.py | |||
@@ -0,0 +1,62 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | # -*- coding: utf-8 -*- | ||
3 | |||
4 | |||
5 | ### Gérer les couleurs | ||
6 | |||
7 | from gestion_erreurs import * | ||
8 | |||
9 | def rgb_vers_tuple(chaine,defaut,liste_err): | ||
10 | """ convertit une chaine rgb genre #00FF1B | ||
11 | vers un tuple python (entre 0 et 1) | ||
12 | En cas d'erreur, on met un warning et on renvoie | ||
13 | le defaut""" | ||
14 | chaine2 = chaine.lstrip("#") | ||
15 | try: | ||
16 | r = int(chaine2[0:2],16)/255 | ||
17 | v = int(chaine2[2:4],16)/255 | ||
18 | b = int(chaine2[4:6],16)/255 | ||
19 | except: | ||
20 | warning("Impossible de convertir la couleur "+chaine,liste_err) | ||
21 | return defaut | ||
22 | return (r,v,b) | ||
23 | |||
24 | def tuple_vers_rgb( t): | ||
25 | """ conversion inverse : renvoie une chaine | ||
26 | de type #FF0045""" | ||
27 | (r,v,b) = t | ||
28 | if (r>1): | ||
29 | r=1 | ||
30 | elif r<0: | ||
31 | r=0 | ||
32 | if (v>1): | ||
33 | v=1 | ||
34 | elif v<0: | ||
35 | v=0 | ||
36 | if (b>1): | ||
37 | b=1 | ||
38 | elif b<0: | ||
39 | b=0 | ||
40 | |||
41 | r = int(r*255) | ||
42 | v = int(v*255) | ||
43 | b = int(b*255) | ||
44 | return '#%02x%02x%02x' % (r,v,b) | ||
45 | |||
46 | def progressif(x,y,nb): | ||
47 | """ renvoie x pour 0, y pour 50, | ||
48 | et entre deux pour les intermédiaires""" | ||
49 | return (x*(50-nb)/50 + y*nb/50) | ||
50 | |||
51 | def degrade_choix(col1,col2,col3,nb): | ||
52 | """ retourne un dégradé entre col1 et col2 (nb<50) | ||
53 | et entre col2 et col3 (nb>50)""" | ||
54 | if nb>100: | ||
55 | nb=100 | ||
56 | elif nb<0: | ||
57 | nb = 0 | ||
58 | |||
59 | if nb<50: | ||
60 | return tuple( progressif(col1[i],col2[i],nb) for i in (0,1,2) ) | ||
61 | else: | ||
62 | return tuple( progressif(col2[i],col3[i],nb-50) for i in (0,1,2) ) \ No newline at end of file | ||