Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New enum: RGBColor #136

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 88 additions & 6 deletions vstools/enums/color.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

from typing import Any, NamedTuple, TypeAlias, Union
from typing import Any, Literal, NamedTuple, TypeAlias, Union

import vapoursynth as vs
from stgpytools import FuncExceptT, classproperty
from stgpytools import CustomEnum, FuncExceptT, classproperty

from ..exceptions import (
ReservedMatrixError, ReservedPrimariesError, ReservedTransferError, UndefinedMatrixError, UndefinedPrimariesError,
UndefinedTransferError, UnsupportedColorRangeError, UnsupportedMatrixError, UnsupportedPrimariesError,
UnsupportedTransferError
NotFoundEnumValue, ReservedMatrixError, ReservedPrimariesError, ReservedTransferError, UndefinedMatrixError,
UndefinedPrimariesError, UndefinedTransferError, UnsupportedColorRangeError, UnsupportedMatrixError,
UnsupportedPrimariesError, UnsupportedTransferError
)
from ..types import HoldsPropValueT, KwargsT
from .stubs import PropEnum, _base_from_video, _ColorRangeMeta, _MatrixMeta, _PrimariesMeta, _TransferMeta
Expand All @@ -22,7 +22,9 @@
'ColorRange',
'ColorRangeT',

'MatrixCoefficients'
'MatrixCoefficients',

'RGBColor',
]


Expand Down Expand Up @@ -1091,6 +1093,86 @@ def is_full(self) -> bool:
return not self.value


class RGBColor(list[float], CustomEnum):
"""An enum representing RGB colours."""

RED = [1.0, 0.0, 0.0]
GREEN = [0.0, 1.0, 0.0]
BLUE = [0.0, 0.0, 1.0]
WHITE = [1.0, 1.0, 1.0]
BLACK = [0.0, 0.0, 0.0]
YELLOW = [1.0, 1.0, 0.0]
MAGENTA = [1.0, 0.0, 1.0]
CYAN = [0.0, 1.0, 1.0]
GRAY = [0.5, 0.5, 0.5]
SILVER = [0.75, 0.75, 0.75]
GOLD = [1.0, 0.84, 0.0]
ORANGE = [1.0, 0.65, 0.0]
PURPLE = [0.5, 0.0, 0.5]
BROWN = [0.65, 0.16, 0.16]
PINK = [1.0, 0.75, 0.8]
LIME = [0.0, 1.0, 0.0]
TEAL = [0.0, 0.5, 0.5]
NAVY = [0.0, 0.0, 0.5]
MAROON = [0.5, 0.0, 0.0]
OLIVE = [0.5, 0.5, 0.0]
INDIGO = [0.29, 0.0, 0.51]
VIOLET = [0.93, 0.51, 0.93]
TURQUOISE = [0.25, 0.88, 0.82]
CORAL = [1.0, 0.5, 0.31]
CRIMSON = [0.86, 0.08, 0.24]
KHAKI = [0.94, 0.9, 0.55]
PLUM = [0.87, 0.63, 0.87]
SALMON = [0.98, 0.5, 0.45]
AQUA = [0.0, 1.0, 1.0]
LAVENDER = [0.9, 0.9, 0.98]
BEIGE = [0.96, 0.96, 0.86]
MINT = [0.24, 0.71, 0.54]
PERIWINKLE = [0.8, 0.8, 1.0]
CHARTREUSE = [0.5, 1.0, 0.0]
BURGUNDY = [0.5, 0.0, 0.13]
MAUVE = [0.88, 0.69, 1.0]
FOREST_GREEN = [0.13, 0.55, 0.13]
RUST = [0.72, 0.25, 0.05]
ROYAL_BLUE = [0.25, 0.41, 0.88]
SLATE = [0.44, 0.5, 0.56]

@classmethod
def from_name(cls, name: str) -> RGBColor:
"""Get the RGBColor from a name."""

if name.upper() not in cls.__members__:
raise NotFoundEnumValue(f'{name} is not a valid RGBColor name.', cls.from_name)

return cls(name)

def to_clip(self, ref: vs.VideoNode | None = None) -> vs.VideoNode:
"""Create a blank clip with the color."""

from .. import core

blank_clip = core.std.BlankClip(ref, format=vs.RGBS, color=self)

if not ref:
return blank_clip

matrix = Matrix.from_video(ref)

return blank_clip.resize.Bicubic(format=ref.format, matrix=matrix)

def scale_value(
self, bitdepth: Literal[
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
]
) -> list[int]:
"""Scale the value of the color."""

from ..utils.scale import scale_value

# TODO: idk what to do with the colour range
return [scale_value(value, 32, bitdepth, range_in=ColorRange.FULL) for value in self.value]


_transfer_matrix_map: dict[Transfer, Matrix] = {
Transfer.SRGB: Matrix.RGB,
Transfer.BT709: Matrix.BT709,
Expand Down
Loading