开发者

Generating pastel colors

I neeed to generate random color. But I need pstel ones. Not too dark, not too bright.

I can generate colors this way:

color = (1..3).to_a.map{ ( c = rand(255).to_s(16) ).size < 2 ? "0#{c}" : c }.to_s

But it will return 开发者_StackOverflowcolors from all palette.


Try this:

start_color = 128 # minimal color amount
total_offset = 64 # sum of individual color offsets above the minimal amount
'#' +
  [0, rand(total_offset), rand(total_offset), total_offset].sort.each_cons(2).map{|a,b|
    "%02x" % (start_color+b-a)
  }.join

Actually, here's tiny Sinatra app that you can play with and see the results instantly:

require 'sinatra'

def get_pastel start_color, total_offset
  '#' +
    [0, rand(total_offset), rand(total_offset), total_offset].sort.each_cons(2).map{|a,b|
      "%02x" % (start_color+b-a)
    }.join
end

get '/:start_color/:total_offset' do |start_color, total_offset|
  (0..20).map{c = get_pastel(start_color.to_i, total_offset.to_i)
    "<span style='background-color:#{c}'>#{c}</span>\n"
  }.join
end

Then fire up the browser and see how it looks:

http://localhost:4567/192/64

http://localhost:4567/128/128

;)


This might give you something useful:

colour_range = 128
colour_brightness = 64
color = (1..3).to_a.map{ ( c = rand(colour_range)+colour_brightness.to_s(16) ).size < 2 ? "0#{c}" : c }.to_s

I think it will limit you to mid saturation, mid-brightness colours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜