Module: Sass::Extras::YUV::Functions

Defined in:
lib/sass/extras/yuv.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/sass/extras/yuv.rb', line 27

def self.included(base)
  base.declare :yuv, [:y, :u, :v]
  base.declare :yuva, [:y, :u, :v, :alpha]
  base.declare :set_brightness, [:color, :amount]
  base.declare :increase_brightness, [:color, :amount]
  base.declare :reduce_brightness, [:color, :amount]
  base.declare :add_brightness, [:color, :amount]
  base.declare :detract_brightness, [:color, :amount]
end

Instance Method Details

#add_brightness(color, amount) ⇒ Object

Parameters:

  • color (Color)
  • amount (Number)

    between 0 and 1.0, or 0% and 100%



94
95
96
# File 'lib/sass/extras/yuv.rb', line 94

def add_brightness(color, amount)
  adjust_brightness(color, amount) { |c, y| [y + c, 1.0].min }
end

#detract_brightness(color, amount) ⇒ Object

Parameters:

  • color (Color)
  • amount (Number)

    between 0 and 1.0, or 0% and 100%



100
101
102
# File 'lib/sass/extras/yuv.rb', line 100

def detract_brightness(color, amount)
  adjust_brightness(color, amount) { |c, y| [y - c, 0.0].max }
end

#increase_brightness(color, amount) ⇒ Object

Parameters:

  • color (Color)
  • amount (Number)

    between 0 and 1.0, or 0% and 100%



82
83
84
# File 'lib/sass/extras/yuv.rb', line 82

def increase_brightness(color, amount)
  adjust_brightness(color, amount) { |c, y| [(1 + c) * y, 1.0].min }
end

#reduce_brightness(color, amount) ⇒ Object

Parameters:

  • color (Color)
  • amount (Number)

    between 0 and 1.0, or 0% and 100%



88
89
90
# File 'lib/sass/extras/yuv.rb', line 88

def reduce_brightness(color, amount)
  adjust_brightness(color, amount) { |c, y| [(1 - c) * y, 0.0].max }
end

#set_brightness(color, amount) ⇒ Object

Parameters:

  • color (Color)
  • amount (Number)

    between 0 and 1.0, or 0% and 100%



76
77
78
# File 'lib/sass/extras/yuv.rb', line 76

def set_brightness(color, amount)
  adjust_brightness(color, amount) { |c, _| c }
end

#yuv(y, u, v) ⇒ Color

Creates a Color object from luma (Y), and two chrominance (UV) values.

Parameters:

  • y (Number)

    A number between 0 and 1.0 inclusive

  • u (Number)

    A number between -U_MAX and +U_MAX inclusive

  • v (Number)

    A number between -V_MAX and +V_MAX inclusive

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sass/extras/yuv.rb', line 46

def yuv(y, u, v)
  assert_type y, :Number, :y
  assert_type u, :Number, :u
  assert_type v, :Number, :v

  yv = y.value
  if y.numerator_units == ["%"] && y.denominator_units.empty?
    fail ArgumentError, "Brightness (Y') value #{y} must be between 0% and 100% inclusive" unless (0..100).include?(y.value)
    yv /= 100.0
  else
    fail ArgumentError, "Brightness (Y') value #{y} must be between 0 and 1.0 inclusive" unless (0..1.0).include?(y.value)
  end
  fail ArgumentError, "Chrominance (U) value #{u} must be between -#{U_MAX} and #{U_MAX} inclusive" unless (-U_MAX..U_MAX).include?(u.value)
  fail ArgumentError, "Chrominance (V) value #{v} must be between -#{V_MAX} and #{V_MAX} inclusive" unless (-V_MAX..V_MAX).include?(v.value)

  r = yv + v.value * (1 - WR) / (V_MAX)
  g = yv - u.value * (WB * (1 - WB)) / (V_MAX * WG) - v.value * (WR * (1 - WR)) / (V_MAX * WG)
  b = yv + u.value * (1 - WB) / U_MAX

  rgb = [r, g, b].map { |c| [0, [255, c * 255].min].max }
  Sass::Script::Color.new(rgb)
end

#yuva(y, u, v, a) ⇒ Object



69
70
71
72
# File 'lib/sass/extras/yuv.rb', line 69

def yuva(y, u, v, a)
  assert_type a, :Number, :a
  yuv(y, u, v).with(alpha: a.value)
end