Skip to content

Commit

Permalink
reorganize expr operators
Browse files Browse the repository at this point in the history
  • Loading branch information
emotion3459 committed Oct 23, 2024
1 parent f2aa143 commit 9c6c24f
Showing 1 changed file with 11 additions and 65 deletions.
76 changes: 11 additions & 65 deletions vsexprtools/exprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from vstools import (
ColorRange, ConvMode, CustomEnum, CustomIndexError, FuncExceptT, HoldsVideoFormatT, PlanesT, StrArrOpt, StrList,
VideoFormatT, VideoNodeIterable, flatten, get_lowest_value, get_neutral_value, get_peak_value, vs, get_sample_type
VideoFormatT, VideoNodeIterable, flatten, get_lowest_value, get_neutral_value, get_peak_value, vs
)

from .util import ExprVarRangeT, ExprVars, ExprVarsT, complexpr_available
Expand All @@ -23,87 +23,33 @@ class ExprTokenBase(str):
class ExprToken(ExprTokenBase, CustomEnum):
LumaMin = 'ymin'
ChromaMin = 'cmin'
Lumamax = 'ymax'
Chromamax = 'cmax'
RangeDiff = 'range_diff'
RangeHalf = 'range_half'
LumaMax = 'ymax'
ChromaMax = 'cmax'
Neutral = 'neutral'
RangeSize = 'range_size'
RangeMin = 'range_min'
LumaRangeMin = 'yrange_min'
ChromaRangeMin = 'crange_min'
RangeMax = 'range_max'
LumaRangeMax = 'yrange_max'
ChromaRangeMax = 'crange_max'
RangeInMin = 'range_in_min'
LumaRangeInMin = 'yrange_in_min'
ChromaRangeInMin = 'crange_in_min'
RangeInMax = 'range_in_max'
LumaRangeInMax = 'yrange_in_max'
ChromaRangeInMax = 'crange_in_max'

@property
def is_chroma(self) -> bool:
return 'chroma' in self._name_.lower()

def get_value(self, clip: vs.VideoNode, chroma: bool = False, range_in: ColorRange = ColorRange.LIMITED) -> float:
if self is ExprToken.LumaMin:
return get_lowest_value(clip, False, ColorRange.LIMITED)

if self is ExprToken.ChromaMin:
return get_lowest_value(clip, True, ColorRange.LIMITED)

if self is ExprToken.Lumamax:
return get_peak_value(clip, False, ColorRange.LIMITED)

if self is ExprToken.Chromamax:
return get_peak_value(clip, True, ColorRange.LIMITED)

if self is ExprToken.RangeDiff:
if get_sample_type(clip) is vs.FLOAT:
return 0.0
return get_neutral_value(clip)

if self is ExprToken.RangeHalf:
return get_neutral_value(clip)

if self is ExprToken.RangeSize:
return (val := get_peak_value(clip)) + (1 - (val <= 1.0))

if self is ExprToken.RangeMin:
return get_lowest_value(clip, chroma)

if self is ExprToken.LumaRangeMin:
return get_lowest_value(clip, False)

if self is ExprToken.ChromaRangeMin:
if self is ExprToken.ChromaMin:
return get_lowest_value(clip, True)

if self is ExprToken.RangeMax:
return get_peak_value(clip, chroma)

if self is ExprToken.LumaRangeMax:
if self is ExprToken.LumaMax:
return get_peak_value(clip, False)

if self is ExprToken.ChromaRangeMax:
if self is ExprToken.ChromaMax:
return get_peak_value(clip, True)

if self is ExprToken.RangeInMin:
return get_lowest_value(clip, chroma, range_in)

if self is ExprToken.LumaRangeInMin:
return get_lowest_value(clip, False, range_in)

if self is ExprToken.ChromaRangeInMin:
return get_lowest_value(clip, True, range_in)

if self is ExprToken.RangeInMax:
return get_peak_value(clip, chroma, range_in)

if self is ExprToken.LumaRangeInMax:
return get_peak_value(clip, False, range_in)
if self is ExprToken.Neutral:
return get_neutral_value(clip)

if self is ExprToken.ChromaRangeInMax:
return get_peak_value(clip, True, range_in)
if self is ExprToken.RangeSize:
return get_peak_value(clip) - get_lowest_value(clip)

def __getitem__(self, __i: SupportsIndex) -> ExprToken: # type: ignore
return ExprTokenBase(f'{self.value}_{ExprVars[__i]}') # type: ignore
Expand Down

0 comments on commit 9c6c24f

Please sign in to comment.