Module: SimpleColor::RGB::ColorNames

Included in:
SimpleColor::RGB
Defined in:
lib/simplecolor/rgb_colors.rb

Instance Method Summary collapse

Instance Method Details

#all_color_namesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simplecolor/rgb_colors.rb', line 64

def all_color_names
	return @rgb_color_names if defined? @rgb_color_names
	# A list of color names, based on X11's rgb.txt
	rgb_colors = File.dirname(__FILE__) + "/../../data/rgb_colors.json.gz"
	# Rewrite file:
	# h={}; rgb.each do |k,v| h[SimpleColor::RGB.rgb_clean(k)]=v end
	# Pathname.new("data/rgb_colors.json").write(h.to_json)
	File.open(rgb_colors, "rb") do |file|
		serialized_data = Zlib::GzipReader.new(file).read
		# serialized_data.force_encoding Encoding::BINARY
		@rgb_color_names = JSON.parse(serialized_data)
	end
end

#color_namesObject



60
61
62
# File 'lib/simplecolor/rgb_colors.rb', line 60

def color_names
	@color_names ||= COLOR_NAMES.dup
end

#color_names_priorityObject



78
79
80
# File 'lib/simplecolor/rgb_colors.rb', line 78

def color_names_priority
	@rgb_color_names.keys
end

#find_color(name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/simplecolor/rgb_colors.rb', line 86

def find_color(name)
	custom=color_names
	case name
	when String
		return custom[name] if custom.key?(name)
		name=rgb_clean(name)
		return custom[name] if custom.key?(name)
		colors=all_color_names
		base, rest=name.split(':', 2)
		if rest.nil?
			color_names_priority.each do |base|
				c=colors[base]
				return c[name] if c.key?(name)
			end
		else
			c=colors[base]
			return c[rest] if c
		end
	else
		custom[name]
	end
end

#rgb_clean(name) ⇒ Object

clean up name



82
83
84
# File 'lib/simplecolor/rgb_colors.rb', line 82

def rgb_clean(name) #clean up name
	name.gsub(/\s+/,'').downcase.gsub('gray','grey')
end