Skip to content

Commit 476a835

Browse files
committed
New way of taking other color schemes
1 parent 99b21d4 commit 476a835

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

app.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
require 'sinatra'
22
require 'githubchart'
33

4-
COLOR_SCHEMES = {
5-
default: ['#eeeeee', '#d6e685', '#8cc665', '#44a340', '#1e6823'],
6-
halloween: ['#EEEEEE', '#FFEE4A', '#FFC501', '#FE9600', '#03001C'],
7-
teal: ['#EEEEEE', "#7FFFD4", "#76EEC6", "#66CDAA", "#458B74"]
8-
}
4+
require './color.rb'
5+
6+
# Examples of nice colors to try out to start with:
7+
# blue: 409ba5
8+
# red: 720100
9+
# green: 44a340
910

1011
get '/' do
1112
send_file File.join(settings.public_folder, 'index.html')
@@ -21,12 +22,13 @@
2122
end
2223
end
2324

24-
get '/:scheme/:username' do
25-
#example: ghchart.rshah.io/teal/2016rshah
26-
#if scheme is not recognized, it will bounce back to the default color scheme
27-
headers 'Content-Type' => "image/svg+xml"
25+
get '/:base/:username' do
26+
headers 'Content-Type' => "image/svg+xml"
2827
username = params[:username].chomp('.svg')
29-
scheme = COLOR_SCHEMES[params[:scheme].to_sym] #must correspond to the color schemes specified above
28+
29+
base_color = '#'+params[:base]
30+
scheme = ['#EEEEEE', lighten_color(base_color, 0.3), lighten_color(base_color, 0.2), base_color, darken_color(base_color, 0.8)]
31+
3032
svg = GithubChart.new(user: username, colors: scheme).svg
3133
stream do |out|
3234
out << svg

color.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#http://www.redguava.com.au/2011/10/lighten-or-darken-a-hexadecimal-color-in-ruby-on-rails/
2+
3+
# COLOR_SCHEMES = {
4+
# default: ['#eeeeee', '#d6e685', '#8cc665', '#44a340', '#1e6823'],
5+
# halloween: ['#EEEEEE', '#FFEE4A', '#FFC501', '#FE9600', '#03001C'],
6+
# teal: ['#EEEEEE', "#7FFFD4", "#76EEC6", "#66CDAA", "#458B74"],
7+
# red: ['#EEEEEE', '#990100', '#8C0100', '#720100', '#4C0100']
8+
# }
9+
10+
# Amount should be a decimal between 0 and 1. Lower means darker
11+
def darken_color(hex_color, amount=0.4)
12+
hex_color = hex_color.gsub('#','')
13+
rgb = hex_color.scan(/../).map {|color| color.hex}
14+
rgb[0] = (rgb[0].to_i * amount).round
15+
rgb[1] = (rgb[1].to_i * amount).round
16+
rgb[2] = (rgb[2].to_i * amount).round
17+
"#%02x%02x%02x" % rgb
18+
end
19+
20+
# Amount should be a decimal between 0 and 1. Higher means lighter
21+
def lighten_color(hex_color, amount=0.6)
22+
hex_color = hex_color.gsub('#','')
23+
rgb = hex_color.scan(/../).map {|color| color.hex}
24+
rgb[0] = [(rgb[0].to_i + 255 * amount).round, 255].min
25+
rgb[1] = [(rgb[1].to_i + 255 * amount).round, 255].min
26+
rgb[2] = [(rgb[2].to_i + 255 * amount).round, 255].min
27+
"#%02x%02x%02x" % rgb
28+
end

0 commit comments

Comments
 (0)