Skip to content

Commit

Permalink
Merge pull request #46 from evvnt/f/dynamic-theme-selection
Browse files Browse the repository at this point in the history
Make theme selection dynamic
  • Loading branch information
rhetprieto authored May 12, 2022
2 parents 7b6c52f + 249d350 commit 5382ac3
Show file tree
Hide file tree
Showing 21 changed files with 9,401 additions and 14,804 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
.rspec_status
/views/theme/node_modules/
/views/theme/npm-debug.log

**/.DS_Store
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GEM
rake (12.3.3)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20
x86_64-linux

Expand Down
104 changes: 21 additions & 83 deletions lib/coprl/presenters/plugins/theme.rb
Original file line number Diff line number Diff line change
@@ -1,90 +1,15 @@
require_relative 'theme/palette'
require_relative 'theme/default_palette'

module Coprl
module Presenters
module Plugins
module Theme
module DSLHelpers

# If you modify a color here, you need to modify the corresponding color in palette.scss
THEMES = {
geotix: {
colors: {
primary10: '#49040E',
primary9: '#790618',
primary8: '#AA0921',
primary7: '#CA0826',
primary6: '#DC1F3C',
primary5: '#F53855',
primary4: '#F6556E',
primary3: '#F98697',
primary2: '#FBB6C1',
primary1: '#FEE7EA',

neutral10: '#090E11',
neutral9: '#1B2932',
neutral8: '#253944',
neutral7: '#324D5C',
neutral6: '#517C95',
neutral5: '#6A96AE',
neutral4: '#8BADC0',
neutral3: '#ACC5D2',
neutral2: '#CDDCE4',
neutral1: '#EBF0F3',

accent14: '#00B587',
accent13: '#00E676',
accent12: '#69F0AE',
accent11: '#B9F6CA',
accent10: '#006064',
accent9: '#00838F',
accent8: '#0097A7',
accent7: '#00ACC1',
accent6: '#00BCD4',
accent5: '#26C6DA',
accent4: '#4DD0E1',
accent3: '#80DEEA',
accent2: '#9EF9E8',
accent1: '#E0F7FA',

red10: '#B71C1C',
red9: '#C62828',
red8: '#D32F2F',
red7: '#E53935',
red6: '#F44336',
red5: '#E55350',
red4: '#E57373',
red3: '#EF9A9A',
red2: '#FFCDD2',
red1: '#FFEBEE',

yellow10: '#F57F17',
yellow9: '#F9A825',
yellow8: '#FBC02D',
yellow7: '#FDD835',
yellow6: '#FFEB3B',
yellow5: '#FFEE58',
yellow4: '#FFF176',
yellow3: '#FFF59D',
yellow2: '#FFF9C4',
yellow1: '#FFFDE7',

green10: '#1B5E20',
green9: '#2E7D32',
green8: '#388E3C',
green7: '#43A047',
green6: '#4CAF50',
green5: '#66BB6A',
green4: '#81C784',
green3: '#A5D6A7',
green2: '#C8E6C9',
green1: '#E8F5E9'
}.freeze
}.freeze
}.freeze

def rgb_color(color_code, theme_code: :geotix)
theme = THEMES.fetch(theme_code) { raise(Errors::ParameterValidation, "Failed to locate theme for: #{theme_code}") }
colors = theme.fetch(:colors) { raise(Errors::ParameterValidation, "No RGB colors defined for: #{theme_code}") }
colors.fetch(color_code) { raise(Errors::ParameterValidation, "Failed to locate color for: #{color_code}") }
def palette_color(color_code, theme = nil)
Palette.new(
primary_color: theme.try(:primary_color), secondary_color: theme.try(:secondary_color)
).palette(color_code)
end
end

Expand All @@ -97,7 +22,20 @@ def view_dir_theme(_pom)
# It will be called once for the page.
# The pom is passed along with the sinatra render method.
def render_header_theme(pom, render:)
render.call :erb, 'theme_header', views: view_dir_theme(pom)
theme = prepare_theme(pom.context)
render.call :erb, 'theme_header', views: view_dir_theme(pom),
locals: { theme: theme, palette: prepare_palette(theme) }
end

def prepare_theme(context)
context[:current_theme].try(:call) || {}
end

def prepare_palette(theme)
Palette.new(
primary_color: theme[:primary_color],
secondary_color: theme[:secondary_color]
)
end
end
end
Expand Down
92 changes: 92 additions & 0 deletions lib/coprl/presenters/plugins/theme/default_palette.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module Coprl
module Presenters
module Plugins
module Theme
class DefaultPalette

COLORS = {
primary10: '#49040E',
primary9: '#790618',
primary8: '#AA0921',
primary7: '#CA0826',
primary6: '#DC1F3C',
primary: '#DC1F3C',
primary5: '#F53855',
primary4: '#F6556E',
primary3: '#F98697',
primary2: '#FBB6C1',
primary1: '#FEE7EA',

neutral10: '#090E11',
neutral9: '#1B2932',
neutral8: '#253944',
neutral7: '#324D5C',
neutral6: '#517C95',
neutral5: '#6A96AE',
neutral4: '#8BADC0',
neutral3: '#ACC5D2',
neutral2: '#CDDCE4',
neutral1: '#EBF0F3',

secondary14: '#00B587',
secondary13: '#00E676',
secondary12: '#69F0AE',
secondary11: '#B9F6CA',
secondary10: '#006064',
secondary9: '#00838F',
secondary8: '#0097A7',
secondary7: '#00ACC1',
secondary6: '#00BCD4',
secondary: '#00ACC1',
secondary5: '#26C6DA',
secondary4: '#4DD0E1',
secondary3: '#80DEEA',
secondary2: '#9EF9E8',
secondary1: '#E0F7FA',

red10: '#B71C1C',
red9: '#C62828',
red8: '#D32F2F',
red7: '#E53935',
red6: '#F44336',
red5: '#E55350',
red4: '#E57373',
red3: '#EF9A9A',
red2: '#FFCDD2',
red1: '#FFEBEE',

yellow10: '#F57F17',
yellow9: '#F9A825',
yellow8: '#FBC02D',
yellow7: '#FDD835',
yellow6: '#FFEB3B',
yellow5: '#FFEE58',
yellow4: '#FFF176',
yellow3: '#FFF59D',
yellow2: '#FFF9C4',
yellow1: '#FFFDE7',

green10: '#1B5E20',
green9: '#2E7D32',
green8: '#388E3C',
green7: '#43A047',
green6: '#4CAF50',
green5: '#66BB6A',
green4: '#81C784',
green3: '#A5D6A7',
green2: '#C8E6C9',
green1: '#E8F5E9'
}.freeze

FONTS = {
default: 'Roboto'
}

def self.[](key)
COLORS[key]
end
end
end
end
end
end
105 changes: 105 additions & 0 deletions lib/coprl/presenters/plugins/theme/palette.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require_relative 'default_palette'

module Coprl
module Presenters
module Plugins
module Theme
class Palette
attr_reader :primary_color, :secondary_color

def initialize(primary_color:, secondary_color:)
@primary_color = primary_color
@secondary_color = secondary_color
end

def palette(color_code)
locate_color(color_code)
end

def [](key)
locate_color(key)
end

private

def locate_color(color_code)
if primary_color.present? && color_code.to_s.starts_with?('primary')
return primary_color_hash[color_code] || DefaultPalette::COLORS[color_code]
end

if secondary_color.present? && color_code.to_s.starts_with?('secondary')
return secondary_color_hash[color_code] || DefaultPalette::COLORS[color_code]
end

return DefaultPalette::COLORS[color_code] if DefaultPalette::COLORS[color_code].present?

raise(Errors::ParameterValidation, "Failed to locate color for: #{color_code}")
end

def primary_color_hash
@primary_color_hash ||= primary_color.present? ? generate_primary_color_hash : {}
end

def secondary_color_hash
@secondary_color_hash ||= secondary_color.present? ? generate_secondary_color_hash : {}
end

def generate_primary_color_hash
{
primary10: darken_color(primary_color, 0.4),
primary9: darken_color(primary_color, 0.5),
primary8: darken_color(primary_color, 0.6),
primary7: darken_color(primary_color, 0.7),
primary6: primary_color,
primary: primary_color,
primary5: lighten_color(primary_color, 0.2),
primary4: lighten_color(primary_color, 0.4),
primary3: lighten_color(primary_color, 0.5),
primary2: lighten_color(primary_color, 0.6),
primary1: lighten_color(primary_color, 0.8)
}
end

def generate_secondary_color_hash
{
secondary14: darken_color(secondary_color, 0.2),
secondary13: darken_color(secondary_color, 0.3),
secondary12: darken_color(secondary_color, 0.4),
secondary11: darken_color(secondary_color, 0.5),
secondary10: darken_color(secondary_color, 0.6),
secondary9: darken_color(secondary_color, 0.7),
secondary8: darken_color(secondary_color, 0.8),
secondary7: darken_color(secondary_color, 0.9),
secondary6: secondary_color,
secondary: secondary_color,
secondary5: lighten_color(secondary_color, 0.2),
secondary4: lighten_color(secondary_color, 0.4),
secondary3: lighten_color(secondary_color, 0.5),
secondary2: lighten_color(secondary_color, 0.6),
secondary1: lighten_color(secondary_color, 0.7)
}
end

def darken_color(hex_color, amount)
hex_color = hex_color.gsub('#','')
rgb = hex_color.scan(/../).map {|color| color.hex}
rgb[0] = (rgb[0].to_i * amount).round
rgb[1] = (rgb[1].to_i * amount).round
rgb[2] = (rgb[2].to_i * amount).round
"#%02x%02x%02x" % rgb
end

# Amount should be a decimal between 0 and 1. Higher means lighter
def lighten_color(hex_color, amount)
hex_color = hex_color.gsub('#','')
rgb = hex_color.scan(/../).map {|color| color.hex}
rgb[0] = [(rgb[0].to_i + 255 * amount).round, 255].min
rgb[1] = [(rgb[1].to_i + 255 * amount).round, 255].min
rgb[2] = [(rgb[2].to_i + 255 * amount).round, 255].min
"#%02x%02x%02x" % rgb
end
end
end
end
end
end
Loading

0 comments on commit 5382ac3

Please sign in to comment.