Skip to content

Color Channels

ColorChannel = Union[PlayerColorChannel, NormalColorChannel, CopiedColorChannel] module-attribute

Represents color channels.

BaseColorChannel

Bases: RobTop

Represents base color channels.

Source code in gd/api/color_channels.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@define()
class BaseColorChannel(RobTop):
    """Represents base color channels."""

    id: int

    @classmethod
    def from_robtop(cls, string: str) -> Self:
        data = split_color_channel(string)

        view = RobTopView(data)

        return cls.from_robtop_view(view)

    @classmethod
    def from_robtop_view(cls, view: RobTopView[int, str]) -> Self:
        id = check_color_channel_id_present(view.get_option(ID).map(int).extract())

        return cls(id=id)

    def to_robtop(self) -> str:
        return concat_color_channel(self.to_robtop_data())

    def to_robtop_data(self) -> Dict[int, str]:
        data = {ID: str(self.id)}

        return data

    def is_player(self) -> bool:
        return False

    def is_normal(self) -> bool:
        return False

    def is_copied(self) -> bool:
        return False

PlayerColorChannel

Bases: BaseColorChannel

Represents player color channels.

Source code in gd/api/color_channels.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
@define()
class PlayerColorChannel(BaseColorChannel):
    """Represents player color channels."""

    player_color: PlayerColor

    opacity: float = DEFAULT_OPACITY

    blending: bool = DEFAULT_BLENDING

    def is_blending(self) -> bool:
        return self.blending

    @classmethod
    def from_robtop_view(cls, view: RobTopView[int, str]) -> Self:
        id = check_color_channel_id_present(view.get_option(ID).map(int).extract())

        player_color = (
            view.get_option(PLAYER_COLOR).map(int).map(PlayerColor).unwrap_or(PlayerColor.DEFAULT)
        )

        opacity = view.get_option(OPACITY).map(float).unwrap_or(DEFAULT_OPACITY)

        blending = view.get_option(BLENDING).map(int_bool).unwrap_or(DEFAULT_BLENDING)

        return cls(id=id, player_color=player_color, opacity=opacity, blending=blending)

    def to_robtop_data(self) -> Dict[int, str]:
        data = super().to_robtop_data()

        here = {
            PLAYER_COLOR: str(self.player_color.value),
            OPACITY: float_str(self.opacity),
            OPACITY_TOGGLED: bool_str(DEFAULT_OPACITY_TOGGLED),
        }

        data.update(here)

        blending = self.is_blending()

        if blending:
            data[BLENDING] = bool_str(blending)

        return data

    def is_player(self) -> bool:
        return True

NormalColorChannel

Bases: BaseColorChannel

Represents normal color channels.

Source code in gd/api/color_channels.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
@define()
class NormalColorChannel(BaseColorChannel):
    """Represents normal color channels."""

    color: Color

    opacity: float = DEFAULT_OPACITY

    blending: bool = DEFAULT_BLENDING

    def is_blending(self) -> bool:
        return self.blending

    @classmethod
    def from_robtop_view(cls, view: RobTopView[int, str]) -> Self:
        id = check_color_channel_id_present(view.get_option(ID).map(int).extract())

        red = view.get_option(RED).map(int).unwrap_or(DEFAULT_RED)
        green = view.get_option(GREEN).map(int).unwrap_or(DEFAULT_GREEN)
        blue = view.get_option(BLUE).map(int).unwrap_or(DEFAULT_BLUE)

        color = Color.from_rgb(red, green, blue)

        opacity = view.get_option(OPACITY).map(float).unwrap_or(DEFAULT_OPACITY)

        blending = view.get_option(BLENDING).map(int_bool).unwrap_or(DEFAULT_BLENDING)

        return cls(id=id, color=color, opacity=opacity, blending=blending)

    def to_robtop_data(self) -> Dict[int, str]:
        data = super().to_robtop_data()

        color = self.color

        here = {
            RED: str(color.red),
            GREEN: str(color.green),
            BLUE: str(color.blue),
            OPACITY: float_str(self.opacity),
            OPACITY_TOGGLED: bool_str(DEFAULT_OPACITY_TOGGLED),
        }

        data.update(here)

        blending = self.is_blending()

        if blending:
            data[BLENDING] = bool_str(blending)

        return data

    def is_normal(self) -> bool:
        return True

CopiedColorChannel

Bases: BaseColorChannel

Represents copied color channels.

Source code in gd/api/color_channels.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
@define()
class CopiedColorChannel(BaseColorChannel):
    """Represents copied color channels."""

    copied_id: int = field()

    hsv: HSV = field(factory=HSV)

    opacity: Optional[float] = field(default=None)

    blending: bool = field(default=DEFAULT_BLENDING)

    def is_blending(self) -> bool:
        return self.blending

    @classmethod
    def from_robtop_view(cls, view: RobTopView[int, str]) -> Self:
        id = check_color_channel_id_present(view.get_option(ID).map(int).extract())

        copied_id = check_color_channel_copied_id_present(
            view.get_option(COPIED_ID).map(int).extract()
        )

        hsv = view.get_option(COLOR_HSV).map(HSV.from_robtop).unwrap_or_else(HSV)

        copy_opacity = view.get_option(COPY_OPACITY).map(int_bool).unwrap_or(DEFAULT_COPY_OPACITY)

        if copy_opacity:
            opacity = None

        else:
            opacity = view.get_option(OPACITY).map(float).unwrap_or(DEFAULT_OPACITY)

        blending = view.get_option(BLENDING).map(int_bool).unwrap_or(DEFAULT_BLENDING)

        return cls(id=id, copied_id=copied_id, hsv=hsv, opacity=opacity, blending=blending)

    def to_robtop_data(self) -> Dict[int, str]:
        data = super().to_robtop_data()

        copy_opacity = self.is_copy_opacity()

        here = {
            COPIED_ID: str(self.copied_id),
            COLOR_HSV: self.hsv.to_robtop(),
            COPY_OPACITY: bool_str(copy_opacity),
            OPACITY_TOGGLED: bool_str(DEFAULT_OPACITY_TOGGLED),
        }

        data.update(here)

        if not copy_opacity:
            data[OPACITY] = float_str(self.opacity_checked)

        blending = self.is_blending()

        if blending:
            data[BLENDING] = bool_str(blending)

        return data

    def is_copied(self) -> bool:
        return True

    @property
    def opacity_checked(self) -> float:
        opacity = self.opacity

        if opacity is None:
            raise ValueError(COPIED_OPACITY)

        return opacity

    def is_copy_opacity(self) -> bool:
        return self.opacity is None

    def copy_opacity(self) -> Self:
        self.opacity = None

        return self

ColorChannels

Bases: Dict[int, ColorChannel]

Represents collections of color channels.

Source code in gd/api/color_channels.py
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
class ColorChannels(Dict[int, ColorChannel]):
    """Represents collections of color channels."""

    def copy(self) -> Self:
        return type(self)(self)

    @classmethod
    def from_color_channel_iterable(cls, color_channels: Iterable[ColorChannel]) -> Self:
        return cls({color_channel.id: color_channel for color_channel in color_channels})

    @classmethod
    def from_color_channels(cls, *color_channels: ColorChannel) -> Self:
        return cls.from_color_channel_iterable(color_channels)

    @property
    def length(self) -> int:
        return len(self)

    def add(self, color_channel: ColorChannel) -> None:
        self[color_channel.id] = color_channel

    @classmethod
    def from_robtop(cls, string: str) -> Self:
        return cls.from_color_channel_iterable(
            iter(split_color_channels(string)).filter(None).map(color_channel_from_robtop).unwrap()
        )

    def to_robtop(self) -> str:
        return concat_color_channels(map(color_channel_to_robtop, self.values()))