Class: Pixelart::Color
- Inherits:
-
Object
- Object
- Pixelart::Color
- Defined in:
- lib/pixelart/color.rb
Constant Summary collapse
- TRANSPARENT =
rgba( 0, 0, 0, 0)
0- BLACK =
rgba( 0, 0, 0,255)
0xff- WHITE =
rgba(255,255,255,255)
0xffffffff- NAMES =
build_names
Class Method Summary collapse
- .b(color) ⇒ Object
-
.build_names ⇒ Object
known built-in color names.
- .format(color) ⇒ Object (also: fmt)
- .from_hex(hex) ⇒ Object
- .from_hsl(hue, saturation, lightness, alpha = 255) ⇒ Object
- .g(color) ⇒ Object
- .parse(color) ⇒ Object
- .r(color) ⇒ Object
- .rgb(r, g, b) ⇒ Object
- .to_hex(color, include_alpha: true) ⇒ Object
- .to_hsl(color, include_alpha: true) ⇒ Object
Class Method Details
.b(color) ⇒ Object
57 |
# File 'lib/pixelart/color.rb', line 57 def self.b( color ) ChunkyPNG::Color.b( color ); end |
.build_names ⇒ Object
known built-in color names
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pixelart/color.rb', line 64 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 |
.format(color) ⇒ Object Also known as: fmt
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pixelart/color.rb', line 85 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 |
.from_hex(hex) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/pixelart/color.rb', line 28 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) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/pixelart/color.rb', line 38 def self.from_hsl( hue, saturation, lightness, alpha=255) ChunkyPNG::Color.from_hsl( hue, saturation, lightness, alpha ) end |
.g(color) ⇒ Object
56 |
# File 'lib/pixelart/color.rb', line 56 def self.g( color ) ChunkyPNG::Color.g( color ); end |
.parse(color) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/pixelart/color.rb', line 11 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) ⇒ Object
55 |
# File 'lib/pixelart/color.rb', line 55 def self.r( color ) ChunkyPNG::Color.r( color ); end |
.rgb(r, g, b) ⇒ Object
59 |
# File 'lib/pixelart/color.rb', line 59 def self.rgb( r, g, b ) ChunkyPNG::Color.rgb( r, g, b); end |
.to_hex(color, include_alpha: true) ⇒ Object
46 47 48 |
# File 'lib/pixelart/color.rb', line 46 def self.to_hex( color, include_alpha: true ) ChunkyPNG::Color.to_hex( color, include_alpha ) end |
.to_hsl(color, include_alpha: true) ⇒ Object
50 51 52 53 |
# File 'lib/pixelart/color.rb', line 50 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 |