Skip to content

Commit

Permalink
create a helper for updating design variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bsatarnejad committed Sep 18, 2024
1 parent f8ac569 commit 75549a4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/helpers/colors_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,28 @@ def highlighted_foreground_light
"color: hsla(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - (var(--color-l) * 0.22)) * 1%), 1) !important;"
end
end

def get_contrast_color(hex)
# Convert hex to RGB
binding.pry

Check warning on line 181 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L181 <Lint/Debugger>

Remove debugger entry point `binding.pry`.
Raw output
app/helpers/colors_helper.rb:181:5: W: Lint/Debugger: Remove debugger entry point `binding.pry`.
color = ColorConversion::Color.new(hex)
rgb = color.rgb

# Calculate luminance
luminance = calculate_luminance(rgb[:r], rgb[:g], rgb[:b])

# Return black or white depending on luminance
luminance > 0.5 ? '#333333' : '#FFFFFF'

Check notice on line 189 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L189 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
app/helpers/colors_helper.rb:189:23: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check notice on line 189 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L189 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
app/helpers/colors_helper.rb:189:35: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end

def calculate_luminance(r, g, b)

Check notice on line 192 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L192 <Naming/MethodParameterName>

Method parameter must be at least 3 characters long.
Raw output
app/helpers/colors_helper.rb:192:27: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.

Check notice on line 192 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L192 <Naming/MethodParameterName>

Method parameter must be at least 3 characters long.
Raw output
app/helpers/colors_helper.rb:192:30: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.

Check notice on line 192 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L192 <Naming/MethodParameterName>

Method parameter must be at least 3 characters long.
Raw output
app/helpers/colors_helper.rb:192:33: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
# Normalize RGB values to the range [0, 1]
r_norm = r / 255.0
g_norm = g / 255.0
b_norm = b / 255.0

# Calculate luminance using the formula
0.2126 * r_norm + 0.7152 * g_norm + 0.0722 * b_norm

Check warning on line 199 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L199 <Lint/AmbiguousOperatorPrecedence>

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
Raw output
app/helpers/colors_helper.rb:199:5: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check warning on line 199 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L199 <Lint/AmbiguousOperatorPrecedence>

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
Raw output
app/helpers/colors_helper.rb:199:23: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check warning on line 199 in app/helpers/colors_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/colors_helper.rb#L199 <Lint/AmbiguousOperatorPrecedence>

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
Raw output
app/helpers/colors_helper.rb:199:41: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
end
# rubocop:enable Layout/LineLength
end
52 changes: 52 additions & 0 deletions app/services/design/update_design_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@ def set_colors
DesignColor.delete_all
end


params[:colors].each do |param_variable, param_hexcode|

Check notice on line 66 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L65-L66 <Layout/EmptyLines>

Extra blank line detected.
Raw output
app/services/design/update_design_service.rb:65:1: C: Layout/EmptyLines: Extra blank line detected.
# design_font_color = DesignColor.find_by(variable: "main-menu-font-color")
# contrast_color = get_contrast_color('#eeeeee')
#
# design_font_color.hexcode = contrast_color
# design_font_color.save

set_font_color(param_variable, param_hexcode);

Check notice on line 73 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L73 <Style/Semicolon>

Do not use semicolons to terminate expressions.
Raw output
app/services/design/update_design_service.rb:73:54: C: Style/Semicolon: Do not use semicolons to terminate expressions.
if design_color = DesignColor.find_by(variable: param_variable)


if param_hexcode.blank?

Check notice on line 77 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L76-L77 <Layout/EmptyLines>

Extra blank line detected.
Raw output
app/services/design/update_design_service.rb:76:1: C: Layout/EmptyLines: Extra blank line detected.
design_color.destroy
elsif design_color.hexcode != param_hexcode
Expand All @@ -85,5 +95,47 @@ def set_theme
def custom_style
@custom_style ||= CustomStyle.current || CustomStyle.create!
end

def set_font_color(color_variable, color_hexcode)
if color_variable === "header-bg-color"
create_update_color("header-item-font-color", color_hexcode)
elsif color_variable === "header-item-bg-hover-color"
create_update_color("header-item-font-hover-color", color_hexcode)
elsif color_variable === "main-menu-bg-color"
create_update_color("main-menu-font-color", color_hexcode)
elsif color_variable === "main-menu-bg-selected-background"
create_update_color("main-menu-selected-font-color", color_hexcode)
elsif color_variable === "main-menu-bg-hover-background"
create_update_color("main-menu-hover-font-color", color_hexcode)
end
end
def create_update_color(color_variable, color_hexcode)

Check notice on line 112 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L112 <Layout/EmptyLineBetweenDefs>

Expected 1 empty line between method definitions; found 0.
Raw output
app/services/design/update_design_service.rb:112:5: C: Layout/EmptyLineBetweenDefs: Expected 1 empty line between method definitions; found 0.
design_font_color = DesignColor.find_by(variable: color_variable)
contrast_color = get_contrast_color(color_hexcode)

design_font_color.hexcode = contrast_color
design_font_color.save
end
def get_contrast_color(hex)

Check notice on line 119 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L119 <Layout/EmptyLineBetweenDefs>

Expected 1 empty line between method definitions; found 0.
Raw output
app/services/design/update_design_service.rb:119:5: C: Layout/EmptyLineBetweenDefs: Expected 1 empty line between method definitions; found 0.
# Convert hex to RGB
color = ColorConversion::Color.new(hex)
rgb = color.rgb

# Calculate luminance
luminance = calculate_luminance(rgb[:r], rgb[:g], rgb[:b])

# Return black or white depending on luminance
luminance > 0.5 ? '#333333' : '#FFFFFF'

Check notice on line 128 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L128 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
app/services/design/update_design_service.rb:128:25: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check notice on line 128 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L128 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
app/services/design/update_design_service.rb:128:37: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end

def calculate_luminance(r, g, b)

Check notice on line 131 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L131 <Naming/MethodParameterName>

Method parameter must be at least 3 characters long.
Raw output
app/services/design/update_design_service.rb:131:29: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.

Check notice on line 131 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L131 <Naming/MethodParameterName>

Method parameter must be at least 3 characters long.
Raw output
app/services/design/update_design_service.rb:131:32: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.

Check notice on line 131 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L131 <Naming/MethodParameterName>

Method parameter must be at least 3 characters long.
Raw output
app/services/design/update_design_service.rb:131:35: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
# Normalize RGB values to the range [0, 1]
r_norm = r / 255.0
g_norm = g / 255.0
b_norm = b / 255.0

# Calculate luminance using the formula
0.2126 * r_norm + 0.7152 * g_norm + 0.0722 * b_norm

Check warning on line 138 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L138 <Lint/AmbiguousOperatorPrecedence>

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
Raw output
app/services/design/update_design_service.rb:138:7: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check warning on line 138 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L138 <Lint/AmbiguousOperatorPrecedence>

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
Raw output
app/services/design/update_design_service.rb:138:25: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check warning on line 138 in app/services/design/update_design_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/services/design/update_design_service.rb#L138 <Lint/AmbiguousOperatorPrecedence>

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
Raw output
app/services/design/update_design_service.rb:138:43: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
end
end
end
9 changes: 8 additions & 1 deletion lib/open_project/custom_styles/design.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ def customizable_variables
accent-color
header-bg-color
header-item-bg-hover-color
header-item-font-color
header-item-font-hover-color
header-border-bottom-color
main-menu-bg-color
main-menu-bg-selected-background
main-menu-bg-hover-background)
main-menu-bg-hover-background
main-menu-font-color
main-menu-selected-font-color
main-menu-hover-font-color
main-menu-border-color)
end
end
end

0 comments on commit 75549a4

Please sign in to comment.