Package horizons :: Package util :: Module color
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.color

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  import horizons.globals 
23 24 25 -class Color:
26 """Class for saving a color. 27 28 Colors are saved in 32 bit rgb-format with an alpha value (for transparency). 32bit mean 29 that each of the for values can only occupy 8 bit, i.e. the value is between 0 and 255. 30 31 Attributes: 32 r, g, b, a: Color values + Alpha 33 name: name of the Color or None 34 """ 35 36 @classmethod
37 - def get_defaults(cls):
38 """Returns an iterator over all available colors in the db. 39 40 for color in Color.get_defaults(): 41 print(color) 42 43 """ 44 colors = horizons.globals.db('SELECT id FROM colors ORDER BY id') 45 return (cls.get(id) for id, in colors)
46 47 @classmethod
48 - def get(cls, key):
49 """Gets a color by name or id from the db. 50 51 Color.get('red') 52 Color.get(5) 53 54 """ 55 query = horizons.globals.db('SELECT red, green, blue FROM colors ' 56 'WHERE name = ? OR id = ?', key, key) 57 try: 58 rgb = query[0] 59 except IndexError: 60 raise KeyError('No color defined for this name or id: {}'.format(key)) 61 else: 62 return cls(*rgb)
63
64 - def __init__(self, r=0, g=0, b=0, a=255):
65 """ 66 @params: int (0, 255) 67 """ 68 self.r, self.g, self.b, self.a = r, g, b, a 69 query = horizons.globals.db('SELECT name, rowid FROM colors ' 70 'WHERE red = ? AND green = ? AND blue = ?', 71 self.r, self.g, self.b) 72 try: 73 # load name for the color, if it's a standard color 74 self.name, self.id = query[0] 75 except IndexError: 76 self.name = None 77 self.id = None
78
79 - def to_tuple(self):
80 """Returns color as (r, g, b)-tuple, where each value is between 0 and 255""" 81 return (self.r, self.g, self.b)
82 83 @property
84 - def is_default_color(self):
85 return self.id is not None
86
87 - def __str__(self):
88 return 'Color' + str(self.to_tuple())
89
90 - def __eq__(self, other):
91 return self.to_tuple() == other.to_tuple()
92
93 - def __hash__(self):
94 return hash("{}{}{}{}".format(self.r, self.g, self.b, self.a))
95