Created
March 14, 2009 06:58
-
-
Save maraigue/79001 to your computer and use it in GitHub Desktop.
かぼちゃミク
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# ミク画像 "kabochamiku-src.png" | |
# (http://f.hatena.ne.jp/maraigue/20081012211841)を、 | |
# かぼちゃミク | |
# (http://f.hatena.ne.jp/maraigue/20081221032946)に | |
# するコード。 | |
# | |
# RMagickが必要 | |
# | |
# 参照:http://blog.livedoor.jp/maraigue/archives/731791.html | |
require "rubygems" | |
require "RMagick" | |
module Magick | |
class Pixel | |
def get_hsv | |
r = red; g = green; b = blue | |
if r > g | |
max = ((b > r) ? b : r); min = ((g > b) ? b : g) | |
else | |
max = ((b > g) ? b : g); min = ((r > b) ? b : r) | |
end | |
# Hue 色相(0-360) | |
if max == min | |
# 未定義 | |
h = 0 | |
else | |
if max == r | |
h = 60 * (g - b) / (max - min) | |
elsif max == g | |
h = 60 * (b - r) / (max - min) + 120 | |
else | |
h = 60 * (r - g) / (max - min) + 240 | |
end | |
end | |
h += 360 if h < 0 | |
# Saturation 彩度(0.0-1.0) | |
s = ((max == 0) ? 0 : ((max - min)*QuantumRange.to_f/max)/QuantumRange) | |
# max == 0 のときは未定義 | |
# Value 明度(0.0-1.0) : max/(RGBそれぞれの最大値)に等しい | |
[h, s, max.to_f/QuantumRange] | |
end | |
def Pixel.from_hsv(h, s, v) | |
range = (h / 60).to_i | |
diff_h = h - 60 * range | |
p = (v * (1.0 - s)) * QuantumRange | |
q = (v * (60 - s * diff_h) / 60) * QuantumRange | |
t = (v * (60 - s * (60 - diff_h)) / 60) * QuantumRange | |
v *= QuantumRange | |
case range | |
when 0; Pixel.new(v, t, p) | |
when 1; Pixel.new(q, v, p) | |
when 2; Pixel.new(p, v, t) | |
when 3; Pixel.new(p, q, v) | |
when 4; Pixel.new(t, p, v) | |
else; Pixel.new(v, p, q) | |
end | |
end | |
end | |
end | |
include Magick | |
img = ImageList.new("kabochamiku-src.png") | |
for y in 0...img.rows | |
for x in 0...img.columns | |
h, s, v = img.pixel_color(x, y).get_hsv | |
if h > 120 && h < 240 && v >= 0.3 | |
h -= 180 | |
h += 360 if h < 0 | |
img.pixel_color(x, y, Pixel.from_hsv(h, s, v)) | |
end | |
end | |
end | |
img.write("kabochamiku-out.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment