Added colours for teams
This commit is contained in:
73
app/helpers/color_helper.rb
Normal file
73
app/helpers/color_helper.rb
Normal file
@@ -0,0 +1,73 @@
|
||||
module ColorHelper
|
||||
# Generates a consistent, visually pleasing color based on an ID
|
||||
# Uses the golden ratio to ensure good color distribution
|
||||
def team_color(team_id)
|
||||
# Use the golden ratio to create a well-distributed sequence of hues
|
||||
golden_ratio_conjugate = 0.618033988749895
|
||||
|
||||
# Use the team_id as a seed for the hue
|
||||
h = (team_id.to_i * golden_ratio_conjugate) % 1
|
||||
|
||||
# Convert to HSL color with fixed saturation and lightness for good UI colors
|
||||
# Saturation: 65% - vibrant but not too intense
|
||||
# Lightness: 55% - visible on both light and dark backgrounds
|
||||
hsl_to_hex(h, 0.65, 0.55)
|
||||
end
|
||||
|
||||
# Returns a color object with various formats for a team
|
||||
# This allows for more flexibility in how the color is used
|
||||
def team_color_object(team_id)
|
||||
h = (team_id.to_i * 0.618033988749895) % 1
|
||||
s = 0.65
|
||||
l = 0.55
|
||||
|
||||
# Calculate RGB values
|
||||
rgb = hsl_to_rgb(h, s, l)
|
||||
hex = hsl_to_hex(h, s, l)
|
||||
|
||||
{
|
||||
hex: hex, # #RRGGBB
|
||||
rgb: "rgb(#{rgb[0]}, #{rgb[1]}, #{rgb[2]})", # rgb(r,g,b)
|
||||
hsl: "hsl(#{(h*360).round}, #{(s*100).round}%, #{(l*100).round}%)", # hsl(h,s%,l%)
|
||||
light_bg: hsl_to_hex(h, s, 0.9), # Lighter version for backgrounds
|
||||
dark_bg: hsl_to_hex(h, s, 0.2), # Darker version for backgrounds
|
||||
border: hsl_to_hex(h, s, 0.4) # Border color
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Convert HSL to Hex color
|
||||
def hsl_to_hex(h, s, l)
|
||||
r, g, b = hsl_to_rgb(h, s, l)
|
||||
"##{r.to_s(16).rjust(2, '0')}#{g.to_s(16).rjust(2, '0')}#{b.to_s(16).rjust(2, '0')}"
|
||||
end
|
||||
|
||||
# Convert HSL to RGB values
|
||||
def hsl_to_rgb(h, s, l)
|
||||
# Convert HSL to RGB using standard algorithm
|
||||
if s == 0
|
||||
r = g = b = (l * 255).round
|
||||
else
|
||||
q = l < 0.5 ? l * (1 + s) : l + s - l * s
|
||||
p = 2 * l - q
|
||||
|
||||
r = (hue_to_rgb(p, q, h + 1/3.0) * 255).round
|
||||
g = (hue_to_rgb(p, q, h) * 255).round
|
||||
b = (hue_to_rgb(p, q, h - 1/3.0) * 255).round
|
||||
end
|
||||
|
||||
[r, g, b]
|
||||
end
|
||||
|
||||
# Helper function for HSL to RGB conversion
|
||||
def hue_to_rgb(p, q, t)
|
||||
t += 1 if t < 0
|
||||
t -= 1 if t > 1
|
||||
|
||||
return p + (q - p) * 6 * t if t < 1/6.0
|
||||
return q if t < 1/2.0
|
||||
return p + (q - p) * (2/3.0 - t) * 6 if t < 2/3.0
|
||||
return p
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user