class Pixelart::Color

Constants

BLACK
NAMES
TRANSPARENT
WHITE

Public Class Methods

b( color ) click to toggle source
# File lib/pixelart/color.rb, line 56
def self.b( color ) ChunkyPNG::Color.b( color ); end
build_names() click to toggle source

known built-in color names

# File lib/pixelart/color.rb, line 63
def self.build_names
  names = {
    '#00000000' => 'TRANSPARENT',
    '#000000ff' => 'BLACK',
    '#ffffffff' => 'WHITE',
  }

  ## auto-add grayscale 1 to 254
  (1..254).each do |n|
    hex = "#" + ('%02x' % n)*3
    hex << "ff"  ## add alpha channel (255)
    names[ hex ] = "8-BIT GRAYSCALE ##{n}"
  end

  names
end
fmt( color )
Alias for: format
format( color ) click to toggle source
# File lib/pixelart/color.rb, line 84
def self.format( color )
  rgb = [r(color),
         g(color),
         b(color)]

  # rgb in hex (string format)
  #   note: do NOT include alpha channel for now - why? why not?
  hex = "#" + rgb.map{|num| '%02x' % num }.join

  hsl = to_hsl( color )
  ## get alpha channel (transparency) for hsla
  alpha = hsl[3]


  buf = ''
  buf << hex
  buf << " / "
  buf << "rgb("
  buf << "%3d " % rgb[0]
  buf << "%3d " % rgb[1]
  buf << "%3d)"  % rgb[2]
  buf << " - "
  buf << "hsl("
  buf << "%3d° " % (hsl[0] % 360)
  buf << "%3d%% " % (hsl[1]*100+0.5).to_i
  buf << "%3d%%)" % (hsl[2]*100+0.5).to_i

  if alpha != 255
    buf << " - α(%3d%%)" % (alpha*100/255+0.5).to_i
  else
    buf << "          "  ## add empty for 255 (full opacity)
  end

  ## note: add alpha channel to hex
  alpha_hex = '%02x' % alpha
  name = NAMES[ hex+alpha_hex ]
  buf << " - #{name}"  if name

  buf
end
Also aliased as: fmt
from_hex( hex ) click to toggle source
# File lib/pixelart/color.rb, line 27
def self.from_hex( hex )
  ## Creates a color by converting it from a string in hex notation.
  ##
  ## It supports colors with (#rrggbbaa) or without (#rrggbb)
  ##  alpha channel as well as the 3-digit short format (#rgb)
  ## for those without. Color strings may include
  ## the prefix "0x" or "#"".
  ChunkyPNG::Color.from_hex( hex )
end
from_hsl( hue, saturation, lightness, alpha=255) click to toggle source
# File lib/pixelart/color.rb, line 37
def self.from_hsl( hue, saturation, lightness, alpha=255)
  ChunkyPNG::Color.from_hsl( hue,
                             saturation,
                             lightness,
                             alpha )
end
g( color ) click to toggle source
# File lib/pixelart/color.rb, line 55
def self.g( color ) ChunkyPNG::Color.g( color ); end
parse( color ) click to toggle source
# File lib/pixelart/color.rb, line 10
def self.parse( color )
  if color.is_a?( Integer )  ## e.g. assumes ChunkyPNG::Color.rgb() or such
    color ## pass through as is 1:1
  elsif color.is_a?( Array )  ## assume array of hsl(a) e. g. [180, 0.86, 0.88]
    from_hsl( *color )
  elsif color.is_a?( String )
    if color.downcase == 'transparent'   ## special case for builtin colors
      TRANSPARENT
    else
      ## note: return an Integer !!! (not a Color class or such!!! )
      from_hex( color )
    end
  else
    raise ArgumentError, "unknown color format; cannot parse - expected rgb hex string e.g. d3d3d3"
  end
end
r( color ) click to toggle source
# File lib/pixelart/color.rb, line 54
def self.r( color ) ChunkyPNG::Color.r( color ); end
rgb( r, g, b ) click to toggle source
# File lib/pixelart/color.rb, line 58
def self.rgb( r, g, b ) ChunkyPNG::Color.rgb( r, g, b); end
to_hex( color, include_alpha: true ) click to toggle source
# File lib/pixelart/color.rb, line 45
def self.to_hex( color, include_alpha: true )
  ChunkyPNG::Color.to_hex( color, include_alpha )
end
to_hsl( color, include_alpha: true ) click to toggle source
# File lib/pixelart/color.rb, line 49
def self.to_hsl( color, include_alpha: true )
  # Returns an array with the separate HSL components of a color.
  ChunkyPNG::Color.to_hsl( color, include_alpha )
end