2010-12-09 14 views
3

Ich brauche Zufallsfarbe. Aber ich brauche PSTEL. Nicht zu dunkel, nicht zu hell.Erzeugen von Pastellfarben

kann ich Farben auf diese Weise erzeugen:

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

Aber es wird Farben aus allen Palette zurück.

+0

Ok, jetzt dieses liest gerade: http://en.wikipedia.org/wiki/Color_theory – fl00r

Antwort

2

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 

Eigentlich ist hier winzige Sinatra App, die Sie sofort die Ergebnisse mit und sehen Sie spielen können:

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 

dann den Browser anwerfen und sehen, wie es aussieht:

http://localhost:4567/192/64

http://localhost:4567/128/128

;)

+0

1 für die Demo. – steenslag

1

Dies könnte Ihnen etwas Nützliches:

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 

Ich denke, es wird Sie begrenzen bis Mitte Sättigung, Helligkeit Mitte Farben.

+0

Ruby Range-Klasse umfasst das Modul Enumerable was bedeutet, dass Ihr hier .to_a Anruf redundant ist./pedantry – noodl

+1

Außerdem ist 'String #%' besser für die Ausgabe von gefüllten Hexadezimalfeldern geeignet. –

+0

Danke für die Kommentare Jungs. Ich habe mich mit der Frage der Farbe beschäftigt, aber den guten Stil und die Anmut übersehen. –