add color to roles, improvement in label color detection

This commit is contained in:
jomo
2015-03-10 00:51:39 +01:00
parent d6221ca8d1
commit b601d49fe6
9 changed files with 30 additions and 37 deletions

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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 %>

View File

@@ -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>

View File

@@ -0,0 +1,5 @@
class AddColorToRoles < ActiveRecord::Migration
def change
add_column :roles, :color, :string
end
end

View File

@@ -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|

View File

@@ -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)