summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjomo <github@jomo.tv>2015-03-10 00:51:39 +0100
committerjomo <github@jomo.tv>2015-03-10 00:51:39 +0100
commitb601d49fe6dee0e5ab23e8f5f188bb2bde3aa2f0 (patch)
tree21dbb0820eeaf19e12c930799f18c6e255a3cf00
parentd6221ca8d198ace4a6f41f3b12f26a84fc82298c (diff)
add color to roles, improvement in label color detection
-rw-r--r--app/assets/stylesheets/style.css.scss14
-rw-r--r--app/helpers/application_helper.rb13
-rw-r--r--app/models/label.rb14
-rw-r--r--app/models/role.rb2
-rw-r--r--app/views/labels/_label.html.erb2
-rw-r--r--app/views/users/_username.html.erb2
-rw-r--r--db/migrate/20150309221221_add_color_to_roles.rb5
-rw-r--r--db/schema.rb3
-rw-r--r--db/seeds.rb12
9 files changed, 30 insertions, 37 deletions
diff --git a/app/assets/stylesheets/style.css.scss b/app/assets/stylesheets/style.css.scss
index 3710d87..f4239b9 100644
--- a/app/assets/stylesheets/style.css.scss
+++ b/app/assets/stylesheets/style.css.scss
@@ -408,7 +408,6 @@ blockquote p {
a.role {
display: inline-block;
- color: #fff !important;
font-weight: bold;
padding: 4px 0.5em;
border-radius: 3px;
@@ -419,20 +418,7 @@ blockquote p {
text-overflow: ellipsis;
max-width: 27em; // works with the 30 character limit
- &.superadmin, &.admin {
- background: #d22 !important;
- }
-
- &.mod {
- background: #6af !important;
- }
-
- &.normal {
- background: #282 !important;
- }
-
&.banned, &.disabled {
- background: #ccc !important;
text-decoration: line-through !important;
color: #888 !important;
}
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 90528fc..a147945 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -69,6 +69,19 @@ module ApplicationHelper
render_youtube(md.render(content))
end
+ # calculate the foreground color
+ # either +dark+ or +light+, based on the +bgcolor+
+ def fcolor(bgcolor = "#000", light = "#fff", dark = "#000")
+ bg = bgcolor.gsub(/[^0-9a-f]/i, "")
+ # convert 3 char to 6 char hex
+ bg.gsub!(/./, '\&\&') if bg.length == 3
+ sum = 0
+ [0, 2, 4].each do |i|
+ sum += bg[i..i+1].to_i(16)
+ end
+ return (sum/3 < 128) ? light : dark
+ end
+
private
def render_youtube(content)
diff --git a/app/models/label.rb b/app/models/label.rb
index 863546d..ee2fb56 100644
--- a/app/models/label.rb
+++ b/app/models/label.rb
@@ -8,20 +8,6 @@ class Label < ActiveRecord::Base
name.upcase
end
- # calculate the foreground color
- # either black or white, based on the bg color
- def fcolor
- bg = color.dup
- # convert 3 char to 6 char hex
- bg.gsub!(/./, '\&\&') if bg.length == 3
- [0, 2, 4].each do |i|
- if bg[i..i+1] >= "7f"
- return "000"
- end
- end
- return "fff"
- end
-
private
def color_valid
diff --git a/app/models/role.rb b/app/models/role.rb
index 47da692..708fb40 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -2,6 +2,8 @@ class Role < ActiveRecord::Base
include Comparable
has_many :users
+ validates :color, format: {with: /#[0-9a-f]{3,6}/i}
+
def to_s
self.name
diff --git a/app/views/labels/_label.html.erb b/app/views/labels/_label.html.erb
index a13fba0..b1ace17 100644
--- a/app/views/labels/_label.html.erb
+++ b/app/views/labels/_label.html.erb
@@ -1,3 +1,3 @@
<% if label %>
- <span class="label label-<%= label.name.downcase %>" style="color: #<%= label.fcolor %>;background-color: #<%= label.color %>"><%= label %></span>
+ <span class="label label-<%= label.name.downcase %>" style="color: <%= fcolor(label.color) %>;background-color: #<%= label.color %>"><%= label %></span>
<% end %> \ No newline at end of file
diff --git a/app/views/users/_username.html.erb b/app/views/users/_username.html.erb
index 7045e73..474c3a0 100644
--- a/app/views/users/_username.html.erb
+++ b/app/views/users/_username.html.erb
@@ -1,4 +1,4 @@
<div class="user">
- <%= link_to user.name, user, class: "role #{user.role.name} #{"banned" if user.banned?} #{"disabled" if user.disabled?} #{"unconfirmed" unless user.confirmed?}", title: user.ign %>
+ <%= link_to user.name, user, class: "role #{user.role.name} #{"banned" if user.banned?} #{"disabled" if user.disabled?} #{"unconfirmed" unless user.confirmed?}", title: user.ign, style: "color: #{fcolor(user.role.color)}; background-color: #{user.role.color}" %>
<%= link_to "$", donate_statics_path, class: "role donor", title: "Donator" if user.donor? %>
</div> \ No newline at end of file
diff --git a/db/migrate/20150309221221_add_color_to_roles.rb b/db/migrate/20150309221221_add_color_to_roles.rb
new file mode 100644
index 0000000..6ce4f0e
--- /dev/null
+++ b/db/migrate/20150309221221_add_color_to_roles.rb
@@ -0,0 +1,5 @@
+class AddColorToRoles < ActiveRecord::Migration
+ def change
+ add_column :roles, :color, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index acd47c1..6cf8ed5 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20150117154652) do
+ActiveRecord::Schema.define(version: 20150309221221) do
create_table "blogposts", force: true do |t|
t.string "title"
@@ -83,6 +83,7 @@ ActiveRecord::Schema.define(version: 20150117154652) do
create_table "roles", force: true do |t|
t.string "name"
t.integer "value"
+ t.string "color"
end
create_table "sessions", force: true do |t|
diff --git a/db/seeds.rb b/db/seeds.rb
index 236540e..556d94c 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -2,12 +2,12 @@
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
Role.create!([
- {name: "disabled", value: 1},
- {name: "banned", value: 2},
- {name: "normal", value: 10},
- {name: "mod", value: 100},
- {name: "admin", value: 200},
- {name: "superadmin", value: 500}
+ {name: "disabled", value: 1, color: "#ccc"},
+ {name: "banned", value: 2, color: "#ccc"},
+ {name: "normal", value: 10, color: "#282"},
+ {name: "mod", value: 100, color: "#6af"},
+ {name: "admin", value: 200, color: "#d22"},
+ {name: "superadmin", value: 500, color: "#d22"}
])
userpw = SecureRandom.hex(64)