~martijnbraam/pyatem

pyatem/gtk_switcher/colorwheel.py -rw-r--r-- 2.8 KiB
ed164087Martijn Braam i18n: update translation files from source 4 days ago
                                                                                
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
import colorsys

import cairo
import io
import math
from gi.repository import Gtk, GObject, Gio


class ColorWheelWidget(Gtk.DrawingArea):
    _gtype_name__ = 'ColorWheelWidget'

    red = GObject.Property(type=float, nick='Red value', default=0.2)
    green = GObject.Property(type=float, nick='Green value', default=0.1)
    blue = GObject.Property(type=float, nick='Blue value', default=0.1)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_size_request(100, 100)
        rainbow = Gio.resources_lookup_data('/nl/brixit/switcher/asset/rainbow.png', 0).get_data()
        rainbowpng = io.BytesIO(rainbow)
        self.rainbow = cairo.ImageSurface.create_from_png(rainbowpng)

    def get_coordinates(self):
        yiq = colorsys.rgb_to_yiq(self.red, self.green, self.blue)
        print(yiq)
        return yiq

    def set_coordinates(self, gain, x, y):
        rgb = colorsys.yiq_to_rgb(gain, x, y)
        self.red, self.green, self.blue = rgb

    def do_draw(self, cr):
        context = self.get_style_context()
        state = self.get_state_flags()
        context.set_state(state)

        size = self.get_allocation()

        # Draw background
        Gtk.render_background(context, cr, 0, 0, size.width, size.height)

        # Draw gain circle
        radius = (min(size.width, size.height) - 2) // 2
        cr.set_line_width(3.0)
        cr.arc(size.width // 2, size.height // 2, radius, 0, math.pi * 2)
        cr.stroke()

        # Draw center cross
        radius = radius * 0.9
        cr.set_line_width(1.0)
        cr.move_to((size.width // 2) - radius, size.height // 2)
        cr.line_to((size.width // 2) + radius, size.height // 2)
        cr.stroke()
        cr.move_to(size.width // 2, (size.height // 2) - radius)
        cr.line_to(size.width // 2, (size.height // 2) + radius)
        cr.stroke()

        # Draw color rainbow circle
        pattern_width = self.rainbow.get_width()
        if size.width > size.height:
            xoffset = (size.width - size.height) // 2
            yoffset = 0
            scale = size.height / pattern_width
        else:
            xoffset = 0
            yoffset = (size.height - size.width) // 2
            scale = size.width / pattern_width
        cr.set_source_surface(self.rainbow, xoffset / scale, yoffset / scale)
        source = cr.get_source()
        matrix = source.get_matrix()
        matrix.xx = 1 / scale
        matrix.yy = 1 / scale
        source.set_matrix(matrix)
        cr.set_line_width(16.0)
        cr.arc(size.width // 2, size.height // 2, radius, 0, math.pi * 2)
        cr.stroke()

        # Draw cursor
        gain, x, y = self.get_coordinates()
        cr.set_source_rgba(0, 0, 0, 1)
        cr.set_line_width(3)
        cr.arc((x * radius) + size.width // 2, (y * radius) + size.height // 2, 6, 0, math.pi * 2)
        cr.stroke()

        cr.save()