color_wheel.sf 657 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Color_wheel
  4. #
  5. require('Imager')
  6. var (width, height) = (300, 300)
  7. var center = Complex(width/2 , height/2)
  8. var img = %O|Imager|.new(
  9. xsize => width,
  10. ysize => height,
  11. )
  12. for y = ^height, x = ^width {
  13. var vector = (center - x - y.i)
  14. var magnitude = (vector.abs * 2 / width)
  15. var direction = ((Num.pi + atan2(vector.real, vector.imag)) / Num.tau)
  16. img.setpixel(
  17. x => x,
  18. y => y,
  19. color => Hash(hsv => [360*direction, magnitude, magnitude < 1 ? 1 : 0])
  20. )
  21. }
  22. img.write(file => 'color_wheel.png')