Skip to content

Commit 34e5b69

Browse files
authored
Merge pull request #164 from joyofrails/css/color-scales
Add color scales
2 parents 4ea7de4 + bbd0f63 commit 34e5b69

16 files changed

+262
-5
lines changed

app/models/color_scale.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
class ColorScale < ApplicationRecord
4+
APP_DEFAULT = {
5+
name: "Custom Cerulean Blue",
6+
weights: {
7+
"50": "#f1f5fd",
8+
"100": "#dfe8fa",
9+
"200": "#c7d7f6",
10+
"300": "#a1beef",
11+
"400": "#749be6",
12+
"500": "#537ade",
13+
"600": "#3e5dd2",
14+
"700": "#354bc0",
15+
"800": "#313f9c",
16+
"900": "#2c397c",
17+
"950": "#1f244c"
18+
}
19+
}.freeze
20+
21+
VALID_WEIGHTS = %w[50 100 200 300 400 500 600 700 800 900 950].freeze
22+
23+
def self.find_or_create_default
24+
find_or_create_by(name: APP_DEFAULT[:name]) do |cs|
25+
APP_DEFAULT[:weights].each do |weight, value|
26+
cs.set_weight(weight, value)
27+
end
28+
end
29+
end
30+
31+
def set_weight(weight, value)
32+
raise ArgumentError, "Invalid weight: #{weight}" unless VALID_WEIGHTS.include?(weight.to_s)
33+
34+
send(:"weight_#{weight}=", value)
35+
end
36+
37+
def weights
38+
VALID_WEIGHTS.each_with_object({}) do |weight, hash|
39+
hash[weight] = send(:"weight_#{weight}")
40+
end
41+
end
42+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class CreateColorScales < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :color_scales, force: true, id: false do |t|
4+
t.primary_key :id, :string, default: -> { "ULID()" }
5+
t.string :name, null: false
6+
t.string :weight_50, null: false
7+
t.string :weight_100, null: false
8+
t.string :weight_200, null: false
9+
t.string :weight_300, null: false
10+
t.string :weight_400, null: false
11+
t.string :weight_500, null: false
12+
t.string :weight_600, null: false
13+
t.string :weight_700, null: false
14+
t.string :weight_800, null: false
15+
t.string :weight_900, null: false
16+
t.string :weight_950, null: false
17+
18+
t.timestamps
19+
end
20+
end
21+
end

db/schema.rb

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/seeds/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
u.password = password
77
u.password_confirmation = password
88
end
9+
10+
ColorScale.find_or_create_default

db/seeds/production.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
u.password = password
77
u.password_confirmation = password
88
end
9+
10+
ColorScale.find_or_create_default

lib/tasks/color_scales/seed.rake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace :color_scales do
2+
desc "Seed ColorScales from JSON files"
3+
task seed: :environment do
4+
# Generate ColorScale rows from precalcated JSON
5+
# "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ...
6+
7+
if !File.exist?("./script/colors/tmp/1500-palette-hex.json")
8+
puts "Run `rake color_scales:generate_1500` first to generate 1500-palette-hex.json"
9+
exit
10+
end
11+
12+
hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json"))
13+
hex_json_1500.each do |name, weights|
14+
ColorScale.find_or_create_by!(name: name) do |cs|
15+
weights.each do |weight, css|
16+
cs.set_weight(weight, css)
17+
end
18+
end
19+
end
20+
21+
hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json"))
22+
hex_json_uicolors.each do |name, weights|
23+
custom_name = "Custom #{name.titleize}"
24+
ColorScale.find_or_create_by!(name: custom_name) do |cs|
25+
weights.each do |weight, css|
26+
cs.set_weight(weight, css)
27+
end
28+
end
29+
end
30+
end
31+
32+
task :generate_1500 do
33+
`node ./script/colors/generate-1500-hex.js > ./script/colors/tmp/1500-palette-hex.json`
34+
end
35+
end

script/uicolors-seed.js renamed to script/colors/data/1500-colors.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const colors = [
1+
/* Credit: https://github.com/PetrochukM/1500-color-names */
2+
export const colors = [
23
['000000', 'Black'],
34
['000080', 'Navy Blue'],
45
['0000C8', 'Dark Blue'],
File renamed without changes.

script/uicolors-hex.json renamed to script/colors/data/uicolors-palette-hex.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"950": "#520527"
139139
},
140140

141-
"siler-tree": {
141+
"silver-tree": {
142142
"50": "#effaf4",
143143
"100": "#d9f2e2",
144144
"200": "#b6e4ca",

script/colors/generate-1500-hex.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Usage: node script/colors/generate-1500-hex.js > script/colors/data/1500-palette-hex.json
2+
//
3+
// This script converts the 1500-colors.mjs file into a JSON object with the
4+
// palette name as the key and a monochrome color scale object as the value.
5+
6+
import { generatePalette } from './palette.mjs';
7+
import { colors } from './data/1500-colors.mjs';
8+
9+
const palettes = colors.reduce((obj, [hex, name]) => {
10+
obj[name] = generatePalette(hex, name);
11+
return obj;
12+
}, {});
13+
14+
console.log(JSON.stringify(palettes));

0 commit comments

Comments
 (0)