Project1

标题: 用Bitmap#hue_change 模拟 HSL颜色渐变 [打印本页]

作者: RPGzh500223    时间: 2022-3-27 14:27
标题: 用Bitmap#hue_change 模拟 HSL颜色渐变
RUBY 代码复制
  1. class Color
  2.   def hue
  3.     r, g, b = self.red / 255, self.green / 255, self.blue / 255
  4.  
  5.     max = min = r
  6.     if g > max then max = g else min = g end
  7.     if b > max then max = b elsif b < min then min = b end
  8.  
  9.     return 0 if max == min
  10.  
  11.     vH = case max
  12.          when r then (g - b) * 60 / (max - min)
  13.          when g then (b - r) * 60 / (max - min) + 120
  14.          when b then (r - g) * 60 / (max - min) + 240
  15.          end
  16.     if vH < 0 then vH += 360 elsif vH > 360 then vH -= 360 end
  17.     vH
  18.   end
  19. end
  20.  
  21.  
  22. class Bitmap
  23.   def draw_scan0(colorA, colorB)
  24.     self.set_pixel(0, 0, colorA)
  25.     return if self.width == 1
  26.     self.set_pixel(0, self.width - 1, colorB)  
  27.     return if self.width == 2
  28.  
  29.     hueA, hueB = colorA.hue, colorB.hue
  30.     deltaHue = (hueB - hueA) / (self.width - 1)
  31.  
  32.     hue, pixel = hueA, Bitmap.new(1, 1)   
  33.  
  34.     1.upto(self.width - 2) do |i|
  35.       pixel.set_pixel(0, 0, colorA)
  36.       pixel.hue_change(hue += deltaHue)
  37.       self.set_pixel(i, 0, pixel.get_pixel(0, 0))
  38.     end
  39.  
  40.     pixel.dispose
  41.  
  42.   end
  43. end
  44.  
  45.  
  46. bitmap = Bitmap.new(640, 1)
  47. red = Color.new(255, 0, 0)
  48. blue = Color.new(0, 0, 255)
  49. count, t = 0, Time.now
  50. loop do
  51.   bitmap.draw_scan0(red, blue)
  52.   break if Time.now - t >= 1.0
  53.   count += 1
  54. end
  55. print "Bitmap#draw_scan0 => #{count}次/秒"
  56.  
  57. sprite = Sprite.new
  58. sprite.bitmap = bitmap
  59. sprite.y = 240
  60. sprite.zoom_y = 20
  61.  
  62. loop {Graphics.update}

作者: guoxiaomi    时间: 2022-3-27 14:52
说起来hue有多个公式,rm用的到底是哪一种呢?反正我图简便就用了坐标旋转……




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1