Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. require "stumpy_png"
  2. class PictureSampler
  3. property canvas : StumpyCore::Canvas,str : String, sampled : Array(Array(Int32)), width , height
  4. def initialize(filename : String ,width : Int32,height : Int32)
  5. @width=width
  6. @canvas=StumpyPNG.read(filename)
  7. @height=height
  8. @sampled=Array(Array(Int32)).new(@canvas.width,Array(Int32).new(@canvas.height,0))
  9. @str=""
  10. puts "initialized"
  11. end
  12. def sampler
  13. (0...@canvas.width).each do |x|
  14. (0...@canvas.height).each do |y|
  15. r, g, b = @canvas[x, y].to_rgb8
  16. @sampled[x][y]=rgbtoansi(r,g,b)
  17. #when puts sampled x y is called here value is different than in toString
  18. end
  19. end
  20. end
  21. def toString
  22. (0...@sampled.size).each do |x|
  23. (0...@sampled[0].size).each do |y|
  24. puts "#{@sampled[x][y]} #{x} #{y}"
  25. @str=@str+colorize(@sampled[x][y]," ")
  26. end
  27. @str=@str+"\n"
  28. end
  29. @str=@str+"\n"
  30. return @str
  31. end
  32.  
  33. private def rgbtoansi(r,g,b)
  34. sample=16
  35. sample+=36*(r/43)
  36. sample+=6*(g/43)
  37. sample+=(b/43)
  38. return sample
  39. end
  40.  
  41. private def colorize(colornumber,text)
  42. #inspired by http://stackoverflow.com/questions/1489183/colorized-ruby-output
  43. "\e[48;5;#{colornumber}m#{text}\e[0m"
  44. end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement