Class: Unmagic::Color::Alpha

Inherits:
Util::Percentage show all
Defined in:
lib/unmagic/color.rb

Overview

Alpha channel for transparency. Stored internally as percentage (0-100%) where 100% is fully opaque and 0% is fully transparent. Use to_css to output as ratio (0.0-1.0) for CSS formats.

Inherits parse method from Percentage which handles:

  • Explicit percentage: “50%” → Alpha with value 50.0

  • Fraction notation: “1/2” → Alpha with value 50.0

  • Bare decimal ≤ 1.0: “0.5” → Alpha with value 50.0 (treated as CSS ratio)

  • Bare decimal > 1.0: “75” → Alpha with value 75.0

Examples:

Parse CSS ratio

alpha = Unmagic::Color::Alpha.parse("0.5")
alpha.value
#=> 50.0

Parse percentage

alpha = Unmagic::Color::Alpha.parse("50%")
alpha.value
#=> 50.0

Constant Summary collapse

DEFAULT =

Default alpha value (fully opaque)

new(value: 100).freeze

Instance Attribute Summary

Attributes inherited from Util::Percentage

#value

Instance Method Summary collapse

Methods inherited from Util::Percentage

#+, #-, #<=>, #==, #abs, build, #initialize, parse, #pretty_print, #to_f, #to_ratio, #to_s, #zero?

Constructor Details

This class inherits a constructor from Unmagic::Util::Percentage

Instance Method Details

#to_cssString

Convert to CSS output format (as ratio 0.0-1.0, not percentage).

Returns “1” for fully opaque, “0” for fully transparent, and decimal values for semi-transparent colors (e.g., “0.5”, “0.75”).

Examples:

Fully opaque

Unmagic::Color::Alpha.new(value: 100).to_css
#=> "1"

Semi-transparent

Unmagic::Color::Alpha.new(value: 50).to_css
#=> "0.5"

Fully transparent

Unmagic::Color::Alpha.new(value: 0).to_css
#=> "0"

Returns:

  • (String)

    The alpha value as a CSS ratio string



353
354
355
356
# File 'lib/unmagic/color.rb', line 353

def to_css
  ratio = to_ratio
  ratio == 1.0 ? "1" : ratio.to_s.sub(/\.?0+$/, "")
end