kin*_*sis 1 ruby hex rmagick colors
我正在使用RMagick制作一个项目,该项目会根据大小和颜色生成随机横幅广告.
第一步是这样,但它无法正常工作.我正在使用所谓的三元语句来使字符串像"#ffffff,#f0f1cd,#123fff"等.
# Generate sixteen random colors
1.upto(16) { |i|
(defined? colors) ? colors << ", #%06x" % (rand(0xffffff)) : colors = "#%06x" % (rand(0xffffff))
}
puts colors.split(',')
Run Code Online (Sandbox Code Playgroud)
期望的结果是不正确的.我希望它分成如下数组:["#fffffff","#f0f1cd","#123fff"]
在最优雅的方法可能.
你可以这样做更容易:
colors = 3.times.map{"%06x" % (rand * 0x1000000)}
Run Code Online (Sandbox Code Playgroud)
注意:如果您使用的是Ruby 1.9.3,则可以使用范围.
colors = 3.times.map{"%06x" % rand(0..0xffffff)}
Run Code Online (Sandbox Code Playgroud)