Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created October 24, 2010 14:09
Show Gist options
  • Save bil-bas/643554 to your computer and use it in GitHub Desktop.
Save bil-bas/643554 to your computer and use it in GitHub Desktop.
module Gosu
class Color
# Setting up for YAML
YAML_DOMAIN = 'libgosu.org,2010'
YAML_TYPE = 'color'
YAML::add_domain_type(YAML_DOMAIN, YAML_TYPE) do |type, value|
p type, value
Gosu::Color.new(*value[:rgba])
end
def to_yaml
"--- #{to_yaml_type} \nrgba: [#{red}, #{green}, #{blue}, #{alpha}]\n"
end
def to_yaml_type
"!#{YAML_DOMAIN}/#{YAML_TYPE}"
end
end
end
Gosu::Color.new(1, 2, 3, 4).to_yaml
YAML.load(Gosu::Color.new(1, 2, 3, 4).to_yaml)
module Gosu
class Color
def to_yaml
"--- !ruby/object:Gosu::Color\nred:#{red}\nblue:#{blue}\ngreen:#{green}\nalpha:#{alpha}\n"
end
end
end
module Gosu
class Color
def to_yaml
"--- !ruby/object:Gosu::Color\ncolor:[#{red},#{green},#{blue},#{alpha}]\n"
end
end
end
Gosu::Color.new(1, 2, 3, 4).to_yaml
#=> "--- !ruby/object:Gosu::Color\nred:2\nblue:4\ngreen:3\nalpha:1\n"
YAML.load(Gosu::Color.new(1, 2, 3, 4).to_yaml)
module Gosu
class Color
# Json serialization.
public
def to_json
{
'json_class' => self.class.name,
'rgba' => [red, green, blue, alpha]
}.to_json
end
public
def self.json_create(attributes)
new(*attributes['rgba'])
end
end
end
Gosu::Color.new(1, 2, 3, 4).to_json
JSON.parse(Gosu::Color.new(1, 2, 3, 4).to_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment