color_wheel.sf 605 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(xsize => width, ysize => height)
  9. define(
  10. PI = Num.pi,
  11. TAU = Num.tau,
  12. )
  13. for y=(^height), x=(^width) {
  14. var vector = (center - x - y.i)
  15. var magnitude = (2*vector.abs / width)
  16. var direction = ((PI + atan2(vector.real, vector.imag)) / TAU)
  17. img.setpixel(x => x, y => y,
  18. color => Hash(hsv => [360*direction, magnitude, magnitude < 1 ? 1 : 0])
  19. )
  20. }
  21. img.write(file => 'color_wheel.png')