Generate Random Colors in Ruby - Hex, RGB & HSL Examples
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Ruby
What: Generate random colors in Ruby using hex, RGB, and HSL formats for Rails applications, web development, and data visualization.
Try it: Use our interactive Colors generator or integrate this code into your Ruby application.
Generate random colors in Ruby using hex, RGB, and HSL formats for Rails applications, web development, and data visualization. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Go or use our interactive web generator.
Ruby Code Example
# Generate Random Hex Color
def random_hex_color
"#%06x" % rand(0x1000000)
end
puts "Hex Color: #{random_hex_color}"
# Generate Random RGB Color
def random_rgb_color
r = rand(256)
g = rand(256)
b = rand(256)
"rgb(#{r}, #{g}, #{b})"
end
puts "RGB Color: #{random_rgb_color}"
# Generate Random HSL Color
def random_hsl_color
h = rand(361)
s = rand(101)
l = rand(101)
"hsl(#{h}, #{s}%, #{l}%)"
end
puts "HSL Color: #{random_hsl_color}"
# Generate Pastel Color
def random_pastel_color
h = rand(361)
s = rand(25..101)
l = rand(70..91)
"hsl(#{h}, #{s}%, #{l}%)"
end
puts "Pastel Color: #{random_pastel_color}"
# Generate Color Palette (Array of hex colors)
def generate_color_palette(count)
count.times.map { random_hex_color }
end
puts "Color Palette: #{generate_color_palette(5).inspect}"
# RGB to Hex Conversion
def rgb_to_hex(r, g, b)
"#%02x%02x%02x" % [r, g, b]
end
puts "RGB to Hex: #{rgb_to_hex(255, 99, 71)}"
# Generate with SecureRandom (for cryptographic security)
require 'securerandom'
def secure_random_hex_color
"#" + SecureRandom.hex(3)
end
puts "Secure Hex: #{secure_random_hex_color}"
[EXPLANATION]
Ruby's rand() generates random numbers. For hex colors, use rand(0x1000000) to generate 0-16777215 and format with "%06x" for 6-digit hex. RGB colors use rand(256) for each channel (0-255). HSL uses hue (0-360), saturation (0-100%), and lightness (0-100%). Ruby's range syntax rand(25..101) is inclusive. Use times.map for generating arrays of colors. SecureRandom.hex(3) generates 6 random hex characters (3 bytes) for cryptographically secure colors.
Expected Output
Hex Color: #8a5c9e RGB Color: rgb(167, 89, 201) HSL Color: hsl(287, 68%, 59%) Pastel Color: hsl(156, 72%, 84%) Color Palette: ["#3d7ea6", "#e85d75", "#ffa600", "#58b368", "#bc5090"] RGB to Hex: #ff6347 Secure Hex: #7b3f9e
Common Use Cases
- Generate colors for Rails view templates
- Create color schemes for Sinatra applications
- Assign colors to categories in data visualizations
- Generate CSS color variables dynamically
- Create random avatar backgrounds
Important Notes
-
rand(n)returns values 0 to n-1 -
Use
SecureRandomfor cryptographic applications -
%is the format operator in Ruby -
times.mapis idiomatic Ruby for generating arrays -
For Rails, consider using
color_fieldform helper
Try Our Interactive Generator
Don't want to write code? Use our free web-based Colors generator with instant results.
TRY COLORS GENERATOR →Other Programming Languages
View Colors generation code examples in PHP
View Colors generation code examples in JavaScript
View Colors generation code examples in Python
View Colors generation code examples in Java
View Colors generation code examples in C#
View Colors generation code examples in C++
View Colors generation code examples in Go