読者です 読者をやめる 読者になる 読者になる

c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programmingに分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡

音樂はSoundCloud等バラバラの場所に公開中です。申し訳ないがlinkをたどるなどして探してください。

考察は現在は主に此のblogで公表中です。

programmingは、ひろくみせるものはGitHubで、個人的なものはBitBucketで開発中です。

c4se

RGBとHSVの色空間を相互変換する。Rubyで

何人目だ。
RGB (red 赤, green 緑, blue 青) とHSV (hue 色相, saturation 彩度, value 明度) の色空間 (color space) を相互変換する。Rubyで。

cf. RGB to HSV conversion | color conversion http://www.rapidtables.com/convert/color/rgb-to-hsv.htm
cf. HSV to RGB conversion | color conversion http://www.rapidtables.com/convert/color/hsv-to-rgb.htm

追記20130812
増えた。
cf. RGBとHSVとHLSとCMYKの色空間を相互変換する。Rubyhttp://c4se.hatenablog.com/entry/2013/08/12/174014

# coding=utf-8
# license: Public Domain

# @param r [Integer] red 0..255
# @param g [Integer] green 0..255
# @param b [Integer] blue 0..255
# @return [Integer[]] [hue, saturation, value]
#   hue 0..360 degree
#   saturation 0..100 %
#   value 0..100 %
def rgb_to_hsv r, g, b
  r /= 255.0
  g /= 255.0
  b /= 255.0
  cmax = [r, g, b].max
  cmin = [r, b, g].min
  d = cmax - cmin
  return [0, 0, (cmax * 100).floor] if d == 0
  h = case cmax
      when r then 60 * ((g - b) / d % 6)
      when g then 60 * ((b - r) / d + 2)
      else        60 * ((r - g) / d + 4)
      end
  [ h,
    d / cmax * 100,
    cmax * 100
    ].map &:floor
end

# @param h [Integer] hue 0..360 degree
# @param s [Integer] saturation 0..100 %
# @param v [Integer] value 0..100 %
# @return [Integer[]] [red, green, blue]
#   red 0..255
#   green 0..255
#   blue 0..255
def hsv_to_rgb h, s, v
  s /= 100.0
  v /= 100.0
  c = v * s
  x = c * (1 - ((h / 60.0) % 2 - 1).abs)
  m = v - c
  r, g, b = case
            when h < 60  then [c, x, 0]
            when h < 120 then [x, c, 0]
            when h < 180 then [0, c, x]
            when h < 240 then [0, x, c]
            when h < 300 then [x, 0, c]
            else              [c, 0, x]
            end
  [r, g, b].map{|channel| ((channel + m) * 255).ceil }
end

puts "RGB to HSV"
puts "black   #{rgb_to_hsv   0,   0,   0} =   0,   0,   0"
puts "white   #{rgb_to_hsv 255, 255, 255} =   0,   0, 100"
puts "red     #{rgb_to_hsv 255,   0,   0} =   0, 100, 100"
puts "lime    #{rgb_to_hsv   0, 255,   0} = 120, 100, 100"
puts "blue    #{rgb_to_hsv   0,   0, 255} = 240, 100, 100"
puts "yellow  #{rgb_to_hsv 255, 255,   0} =  60, 100, 100"
puts "cyan    #{rgb_to_hsv   0, 255, 255} = 180, 100, 100"
puts "magenta #{rgb_to_hsv 255,   0, 255} = 300, 100, 100"
puts "silver  #{rgb_to_hsv 192, 192, 192} =   0,   0,  75"
puts "gray    #{rgb_to_hsv 128, 128, 128} =   0,   0,  50"
puts "maroon  #{rgb_to_hsv 128,   0,   0} =   0, 100,  50"
puts "olive   #{rgb_to_hsv 128, 128,   0} =  60, 100,  50"
puts "green   #{rgb_to_hsv   0, 128,   0} = 120, 100,  50"
puts "purple  #{rgb_to_hsv 128,   0, 128} = 300, 100,  50"
puts "teal    #{rgb_to_hsv   0, 128, 128} = 180, 100,  50"
puts "navy    #{rgb_to_hsv   0,   0, 128} = 240, 100,  50"

puts "HSV to RGB"
puts "black   #{hsv_to_rgb   0,   0,   0} =   0,   0,   0"
puts "white   #{hsv_to_rgb   0,   0, 100} = 255, 255, 255"
puts "red     #{hsv_to_rgb   0, 100, 100} = 255,   0,   0"
puts "lime    #{hsv_to_rgb 120, 100, 100} =   0, 255,   0"
puts "blue    #{hsv_to_rgb 240, 100, 100} =   0,   0, 255"
puts "yellow  #{hsv_to_rgb  60, 100, 100} = 255, 255,   0"
puts "cyan    #{hsv_to_rgb 180, 100, 100} =   0, 255, 255"
puts "magenta #{hsv_to_rgb 300, 100, 100} = 255,   0, 255"
puts "silver  #{hsv_to_rgb   0,   0,  75} = 192, 192, 192"
puts "gray    #{hsv_to_rgb   0,   0,  50} = 128, 128, 128"
puts "maroon  #{hsv_to_rgb   0, 100,  50} = 128,   0,   0"
puts "olive   #{hsv_to_rgb  60, 100,  50} = 128, 128,   0"
puts "green   #{hsv_to_rgb 120, 100,  50} =   0, 128,   0"
puts "purple  #{hsv_to_rgb 300, 100,  50} = 128,   0, 128"
puts "teal    #{hsv_to_rgb 180, 100,  50} =   0, 128, 128"
puts "navy    #{hsv_to_rgb 240, 100,  50} =   0,   0, 128"