Color.__add__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
"""
0 ignored issues
show
Documentation introduced by
Empty module docstring
Loading history...
2
3
"""
4
5
from __future__ import absolute_import
6
7
from tcod.libtcod import ffi, lib
8
9
10
class Color(list):
11
    """
12
13
    Args:
14
        r (int): Red value, from 0 to 255.
15
        g (int): Green value, from 0 to 255.
16
        b (int): Blue value, from 0 to 255.
17
    """
18
19
    def __init__(self, r=0, g=0, b=0):
0 ignored issues
show
Bug introduced by
The __init__ method of the super-class list is not called.

It is generally advisable to initialize the super-class by calling its __init__ method:

class SomeParent:
    def __init__(self):
        self.x = 1

class SomeChild(SomeParent):
    def __init__(self):
        # Initialize the super class
        SomeParent.__init__(self)
Loading history...
20
        self[:] = (r & 0xff, g & 0xff, b & 0xff)
21
22
    @property
23
    def r(self):
0 ignored issues
show
Coding Style Naming introduced by
The name r does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
24
        """int: Red value, always normalised to 0-255."""
25
        return self[0]
26
27
    @r.setter
28
    def r(self, value):
0 ignored issues
show
Coding Style Naming introduced by
The name r does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
29
        self[0] = value & 0xff
30
31
    @property
32
    def g(self):
0 ignored issues
show
Coding Style Naming introduced by
The name g does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
33
        """int: Green value, always normalised to 0-255."""
34
        return self[1]
35
    @g.setter
36
    def g(self, value):
0 ignored issues
show
Coding Style Naming introduced by
The name g does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
37
        self[1] = value & 0xff
38
39
    @property
40
    def b(self):
0 ignored issues
show
Coding Style Naming introduced by
The name b does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
41
        """int: Blue value, always normalised to 0-255."""
42
        return self[2]
43
    @b.setter
44
    def b(self, value):
0 ignored issues
show
Coding Style Naming introduced by
The name b does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
45
        self[2] = value & 0xff
46
47
    @classmethod
48
    def _new_from_cdata(cls, cdata):
49
        """new in libtcod-cffi"""
50
        return cls(cdata.r, cdata.g, cdata.b)
51
52
    def __getitem__(self, index):
53
        try:
54
            return list.__getitem__(self, index)
55
        except TypeError:
56
            return list.__getitem__(self, 'rgb'.index(index))
57
58
    def __setitem__(self, index, value):
59
        try:
60
            list.__setitem__(self, index, value)
61
        except TypeError:
62
            list.__setitem__(self, 'rgb'.index(index), value)
63
64
    def __eq__(self, other):
65
        """Compare equality between colors.
66
67
        Also compares with standard sequences such as 3-item tuples or lists.
68
        """
69
        try:
70
            return bool(lib.TCOD_color_equals(self, other))
71
        except TypeError:
72
            return False
73
74
    def __add__(self, other):
75
        """Add two colors together."""
76
        return Color._new_from_cdata(lib.TCOD_color_add(self, other))
77
78
    def __sub__(self, other):
79
        """Subtract one color from another."""
80
        return Color._new_from_cdata(lib.TCOD_color_subtract(self, other))
81
82
    def __mul__(self, other):
83
        """Multiply with a scaler or another color."""
84
        if isinstance(other, (Color, list, tuple)):
85
            return Color._new_from_cdata(lib.TCOD_color_multiply(self, other))
86
        else:
87
            return Color._new_from_cdata(
88
                lib.TCOD_color_multiply_scalar(self, other))
89
90
    def __repr__(self):
91
        """Return a printable representation of the current color."""
92
        return "%s(%i,%i,%i)" % (self.__class__.__name__,
93
                                 self.r, self.g, self.b)
94
95
96
def _import_colors(lib):
0 ignored issues
show
Comprehensibility Bug introduced by
lib is re-defining a name which is already available in the outer-scope (previously defined on line 7).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
97
    """Import all Color constants from lib into this module."""
98
    g = globals()
0 ignored issues
show
Coding Style Naming introduced by
The name g does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
99
    for name in dir(lib):
100
        if name[:5] != 'TCOD_':
101
            continue
102
        value = getattr(lib, name)
103
        if not isinstance(value, ffi.CData):
104
            continue
105
        if ffi.typeof(value) != ffi.typeof('TCOD_color_t'):
106
            continue
107
        g[name[5:]] = Color._new_from_cdata(value)
108
109
110
_import_colors(lib)
111