Create pastel colors

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

I can generate colors as follows:

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

But it will return colors from any palette.

+3
source share
2 answers

Try the following:

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

In fact, here is a tiny Sinatra app with which you can play and instantly see the results:

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 launch the browser and see how it looks:

http: // localhost: 4567/192/64

http: // localhost: 4567/128/128

;)

+2
source

This may 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 this will limit you to medium saturation, medium brightness colors.

+1
source

Source: https://habr.com/ru/post/1779413/


All Articles