Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_colorgamut_triadic.py
blob: af6c92a3343ad8eeff18d2cae77ec9aa76126f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding: UTF-8
# Copyright 2009, 2010 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""Extension point for color constraints.
This module handles a continuous red, green, blue and alpha color space."""

import random
import colorsys
import model_random
import model_locus
import exon_color
from gettext import gettext as _

class TriadicColorGamut(model_locus.Locus):
    """Manages a series of monochromatic colors."""

    def __init__(self, trunk):
        """Color gamut constructor."""
        super(TriadicColorGamut, self).__init__(trunk)
        self.hue = 0.0
        
    def __eq__(self, other):
        """Equality based on the gamuts components."""
        equal = isinstance(other, TriadicColorGamut) \
                and self.hue == other.hue
        return equal

    def randomize(self):
        """Set hue.
        """
        self.hue = random.random()

    def get_randomized_color(self, path):
        """Set red, green, blue and alpha to random values.
        """
        hue_dircetion = random.choice([-1, 0, 1])
        hue = self._get_hue(hue_dircetion)
        lightness = random.random()
        saturation = random.random()
        alpha = random.random()
        rgb = colorsys.hls_to_rgb(hue, lightness, saturation)
        color = exon_color.Color(path, rgb[0], rgb[1], rgb[2], alpha)
        color.set_base_color(hue_dircetion, 0, 0)
        return color

    def mutate(self):
        """Make small random changes in hue.
        """
        self.hue = model_random.cyclic_limit(self.hue + model_random.jitter(0.1))

    def adjust_color(self, color):
        """Adjust rgba value to mutated hue.
        pre: len(color.rgba) == 4
        """
        rgba = color.rgba
        dummy, lightness, saturation = colorsys.rgb_to_hls( \
                                  rgba[0], rgba[1], rgba[2])
        hue = self._get_hue(color.base_diff_hue)
        rgb = colorsys.hls_to_rgb(hue, lightness, saturation)
        color.rgba = (rgb[0], rgb[1], rgb[2], rgba[3])

    def explain(self, formater):
        formater.text_item(_('Triadic color scheme'))

    def copy(self):
        """The TriadicColorGamut copy constructor
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        """
        new_one = TriadicColorGamut(self.get_trunk())
        new_one.hue = self.hue
        return new_one

    def _get_hue(self, hue_dircetion):
        if hue_dircetion < 0:
            hue = model_random.cyclic_limit(self.hue - (120.0 / 360.0))
        elif hue_dircetion > 0:
            hue = model_random.cyclic_limit(self.hue + (120.0 / 360.0))
        else:
            hue = self.hue
        return hue