Skip to content

Commit bbd0f63

Browse files
committed
Add scripts to generate color scales
1 parent 22cf3cb commit bbd0f63

File tree

10 files changed

+124
-8
lines changed

10 files changed

+124
-8
lines changed

app/models/color_scale.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,4 @@ def weights
3939
hash[weight] = send(:"weight_#{weight}")
4040
end
4141
end
42-
43-
def to_css
44-
weights.map { |weight, value| "--color-#{name.dasherize}-#{weight}: #{value};" }.join("\n")
45-
end
4642
end

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

script/colors/palette.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import chroma from 'chroma-js';
2+
3+
export const getColor = (color) => chroma(color);
4+
5+
export const generateScale = (color) => {
6+
const colors = chroma.scale(['white', color, 'black']);
7+
const palette = [];
8+
9+
// Create 50
10+
palette.push(colors(0.05).hex());
11+
12+
// Create 100-900
13+
for (let i = 0.1; i < 0.9; i += 0.1) {
14+
palette.push(colors(i).hex());
15+
}
16+
17+
// Create 950
18+
palette.push(colors(0.95).hex());
19+
20+
return palette;
21+
};
22+
23+
export const isValid = (color) => chroma.valid(color);
24+
25+
export const generatePalette = (color, name = 'myColor') => {
26+
const palette = generateScale(color);
27+
return {
28+
50: palette[0],
29+
100: palette[1],
30+
200: palette[2],
31+
300: palette[3],
32+
400: palette[4],
33+
500: palette[5],
34+
600: palette[6],
35+
700: palette[7],
36+
800: palette[8],
37+
900: palette[9],
38+
950: palette[10],
39+
};
40+
};
41+
42+
export const generateColorPalette = (color, name = 'myColor') => {
43+
return {
44+
[name]: generatePalette(color),
45+
};
46+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "json"
5+
require "color_conversion"
6+
require "active_support/deprecation"
7+
require "active_support/deprecator"
8+
require "active_support/core_ext/string"
9+
10+
Color = ColorConversion::Color
11+
12+
filename = ARGV.shift
13+
raise "Usage: #{$0} <filename>" unless filename
14+
15+
hex_json = JSON.parse(File.read(filename))
16+
17+
hsl_json = hex_json.sort_by { |color, _| color }.each_with_object({}) do |(color, weight_hex_pairs), config|
18+
config[color] = weight_hex_pairs.map do |weight, hex|
19+
hsl = Color.new(hex).hsl
20+
[weight, "hsla(#{hsl[:h]}, #{hsl[:s]}%, #{hsl[:l]}%, 1)"]
21+
end.to_h
22+
end
23+
24+
puts hsl_json.to_json

script/rewrite-tailwind-colors-hsla renamed to script/colors/rewrite-tailwind-colors-hsla

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ weight = nil
1111

1212
puts ":root {"
1313

14-
File.readlines("./tailwind-colors-hex").each do |line|
14+
File.readlines("./input/tailwind-colors-hex").each do |line|
1515
value = line.strip
1616
if line.start_with?("#")
1717
hex = value

script/rewrite-uicolors-hsla renamed to script/colors/rewrite-uicolors-hsla

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ puts "/* This file is generated by script/rewrite-uicolors-hsla.rb */"
1313
puts "/* Credit: Color palettes by https://uicolors.app */"
1414
puts ":root {"
1515

16-
color_json = JSON.parse(File.read("./script/uicolors-hex.json"))
16+
color_json = JSON.parse(File.read(File.join(File.dirname(__FILE__), "./data/uicolors-palette-hex.json")))
1717

1818
color_json.sort_by { |color, _| color }.each do |color, weight_hex_pairs|
1919
puts "\s\s/* #{color} */"

0 commit comments

Comments
 (0)