Class: RGhost::RGB

Inherits:
Color show all
Defined in:
lib/rghost/color.rb

Overview

Creates RGB color

Constant Summary collapse

CONSTANTS =
RGhost::Constants::Colors::RGB
DEFAULT_RGB =
{:red => 0, :green => 0, :blue => 0}

Constants included from RubyToPs

RGhost::RubyToPs::UTF8_ENCODINGS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Color

create

Methods included from RubyToPs

#array_to_stack, #hash_to_array, #pack_string, #ps_escape, #string_eval, #to_array, #to_bool, #to_string, #to_string_array

Methods inherited from PsObject

#<<, #call, #graphic_scope, #raw, #set, #to_s

Constructor Details

#initialize(color_or_red = nil, green = nil, blue = nil) ⇒ RGB

String HTML color converter Color.create ‘#FFAA33’ As Symbol will be find in RGhost::Constants::Colors::RGB Color.create :red

As Array with 3 elements Color.create [0.5, 0.3, 0.5] Hash with 3 pair of key/value. Valids keys :red, :green and :blue Color.create :red => 0.5, :green => 0.3, :blue => 0.5 Hash with 3 pair of key/value. Valids keys :r, :g and :b Color.create :r => 0.5, :g => 0.3, :b => 0.5



79
80
81
82
83
84
# File 'lib/rghost/color.rb', line 79

def initialize(color_or_red=nil,green=nil,blue=nil)
  @color=color_or_red
  @color=[color_or_red.to_f,green.to_f,blue.to_f] if color_or_red.is_a? Numeric
  @color=DEFAULT_RGB.merge(color_or_red) if color_or_red.is_a? Hash
  
end

Instance Attribute Details

#blueObject

Returns the value of attribute blue.



66
67
68
# File 'lib/rghost/color.rb', line 66

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



66
67
68
# File 'lib/rghost/color.rb', line 66

def green
  @green
end

#redObject

Returns the value of attribute red.



66
67
68
# File 'lib/rghost/color.rb', line 66

def red
  @red
end

Instance Method Details

#color_paramsObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/rghost/color.rb', line 94

def color_params
  case @color
  when Hash then   [@color[:r] || @color[:red], @color[:g] || @color[:green],@color[:b] || @color[:blue]]
  when Array then  @color
  when String then hex_to_rgb(@color) 
  when NilClass then [0,0,1]
  end


end

#hex_to_rgb(color = "#FFFFFF") ⇒ Object



105
106
107
108
# File 'lib/rghost/color.rb', line 105

def hex_to_rgb(color="#FFFFFF")

  color.gsub(/#/,'').scan(/[\dA-F]{2}/).map{|h| h.hex / 255.0}
end

#psObject



86
87
88
89
90
# File 'lib/rghost/color.rb', line 86

def ps
value=color_params
  
  array_to_stack(value.map{|n| n > 1 ? n/100.0: n})+"setrgbcolor"
end

#stack_formatObject



91
92
93
# File 'lib/rghost/color.rb', line 91

def stack_format
	color_params
end