ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::Color

Inherits:
Object
  • Object
show all

Overview

RGBA colors. The integers in the constructor are handled as bytes, floats are multiplied by 255, truncated, and clamped to the range 0–255.

Examples:

ModraUp::Color.new(255, 0, 0).red  #=> 255

Class Method Summary # collapse

  • .new ⇒ ModraUp::Color
    Construct a color. With no parameters, it initializes to black and opaque. The name can be a name from ModraUp::Color.names (case-insensitive), or a CSS hexadecimal value (#rgb / #rrggbb). Each channel can accept an integer or a float (e.g., 0.5 → 127), with values clamped to the range 0–255. A single integer is parsed as 0xBBGGRR, with higher bits ignored, and alpha is set to 255.
  • .names ⇒ Array<String>
    Known color names (from the SketchUp Materials Browser / CSS table). ModraUp::View#drawing_color= also recognizes these names.

Instance Method Summary # collapse

  • #red ⇒ Integer
    Red channel (0–255).
  • #green ⇒ Integer
    Green channel (0–255).
  • #blue ⇒ Integer
    Blue channel (0–255).
  • #alpha ⇒ Integer
    Opacity channel (0–255).
  • #red= ⇒ Numeric
    Set the red channel. Decimal values are truncated to bytes, with results ranging from 0–255.
  • #green= ⇒ Numeric
    Set the green channel. Decimal values are truncated to the nearest byte, and the result must be in the range 0–255.
  • #blue= ⇒ Numeric
    Set the blue channel. Decimal values are truncated to bytes, with results ranging from 0–255.
  • #alpha= ⇒ Numeric
    Set opacity. Integer must be in the range 0–255, Float must be in the range 0.0–1.0, scaled to bytes (0.5 → 127).
  • #to_a ⇒ Array<Integer>
    Returns [red, green, blue, alpha].
  • #to_i ⇒ Integer
    Returns an integer in the format 0xBBGGRR, excluding alpha.
  • #to_s ⇒ String
    Returns a four-channel description, such as Color( 18, 52, 86, 255).
  • #blend(other, weight) ⇒ ModraUp::Color
    Returns a new color, blending the RGBA channels with the formula: +new_color = +self + weight * (other - self) +, where fractional parts are truncated and the result is clamped to the range 0–255. The input colors are not modified; the other color can be any value accepted by the constructor.
  • #== ⇒ Boolean
    If all four channels are equal, it returns true.

Class Method Details

Color.new ⇒ colorColor.new(red, green, blue) ⇒ colorColor.new(red, green, blue, alpha) ⇒ colorColor.new([red, green, blue]) ⇒ colorColor.new(name) ⇒ colorColor.new(color) ⇒ colorColor.new(integer) ⇒ color

Construct a color. With no parameters, it initializes to black and opaque. The name can be a name from ModraUp::Color.names (case-insensitive), or a CSS hexadecimal value (#rgb / #rrggbb). Each channel can accept an integer or a float (e.g., 0.5 → 127), with values clamped to the range 0–255. A single integer is parsed as 0xBBGGRR, with higher bits ignored, and alpha is set to 255.

Parameters:

  • red (Numeric)
  • green (Numeric)
  • blue (Numeric)
  • alpha (Numeric) Default: 255; integer 0–255, or decimal 0.0–1.0
  • name (String) Color name or a CSS color value such as #rrggbb.

Returns:

  • (ModraUp::Color)

Raises:

  • [ArgumentError] Incorrect number of arguments or unknown color name

Examples:

ModraUp::Color.new(255, 128, 0).to_a  #=> [255, 128, 0, 255]
ModraUp::Color.new(0, 128, 255, 0.5).alpha  #=> 127
ModraUp::Color.new("DarkGray").red  #=> 169
ModraUp::Color.new("#1A1A1A").to_a  #=> [26, 26, 26, 255]

Color.names ⇒ Array<String>

Known color names (from the SketchUp Materials Browser / CSS table). ModraUp::View#drawing_color= also recognizes these names.

Returns:

  • (Array<String>)

Examples:

ModraUp::Color.names.include?("Red")

Instance Method Details

color.red ⇒ Integer

Red channel (0–255).

Returns:

  • (Integer)

Examples:

ModraUp::Color.new(255, 0, 0).red  #=> 255

color.green ⇒ Integer

Green channel (0–255).

Returns:

  • (Integer)

Examples:

ModraUp::Color.new(0, 128, 0).green  #=> 128

color.blue ⇒ Integer

Blue channel (0–255).

Returns:

  • (Integer)

Examples:

ModraUp::Color.new(0, 0, 64).blue  #=> 64

color.alpha ⇒ Integer

Opacity channel (0–255).

Returns:

  • (Integer)

Examples:

ModraUp::Color.new(0, 0, 0, 128).alpha  #=> 128

color.red = value ⇒ value

Set the red channel. Decimal values are truncated to bytes, with results ranging from 0–255.

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

Examples:

c = ModraUp::Color.new
c.red = 200

color.green = value ⇒ value

Set the green channel. Decimal values are truncated to the nearest byte, and the result must be in the range 0–255.

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

Examples:

c = ModraUp::Color.new
c.green = 10

color.blue = value ⇒ value

Set the blue channel. Decimal values are truncated to bytes, with results ranging from 0–255.

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

Examples:

c = ModraUp::Color.new
c.blue = 10

color.alpha = value ⇒ value

Set opacity. Integer must be in the range 0–255, Float must be in the range 0.0–1.0, scaled to bytes (0.5 → 127).

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)

Examples:

c = ModraUp::Color.new("red")
c.alpha = 64
c.alpha = 0.5
c.alpha  #=> 127

color.to_a ⇒ Array<Integer>

Returns [red, green, blue, alpha].

Returns:

  • (Array<Integer>)

Examples:

ModraUp::Color.new(1, 2, 3).to_a  #=> [1, 2, 3, 255]

color.to_i ⇒ Integer

Returns an integer in the format 0xBBGGRR, excluding alpha.

Returns:

  • (Integer)

color.to_s ⇒ String

Returns a four-channel description, such as Color( 18, 52, 86, 255).

Returns:

  • (String)

color.blend(other, weight) ⇒ ModraUp::Color

Returns a new color, blending the RGBA channels with the formula: +new_color = +self + weight * (other - self) +, where fractional parts are truncated and the result is clamped to the range 0–255. The input colors are not modified; the other color can be any value accepted by the constructor.

Parameters:

  • other (ModraUp::Color, String, Array, Integer)
  • weight (Numeric) The weight of the current color for interpolation; returns transparent black for values out of range.

Returns:

  • (ModraUp::Color)

Examples:

ModraUp::Color.new(255, 75, 75).blend([255, 0, 0], 0.5).to_a
#=> [255, 37, 37, 255]

color == other ⇒ Boolean

If all four channels are equal, it returns true.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Examples:

ModraUp::Color.new(1, 2, 3) == ModraUp::Color.new(1, 2, 3)