Skip to content

Commit 2a5f1a0

Browse files
committed
Refactoring
1 parent c3527b6 commit 2a5f1a0

2 files changed

Lines changed: 46 additions & 46 deletions

File tree

assets/stylesheets/global/_icons.scss.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
<%=
3333
items = []
3434

35-
manifest['icons'].each do |icon|
35+
manifest['items'].each do |item|
3636
rules = []
37-
rules << "background-position: -#{icon['col']}rem -#{icon['row']}rem;"
38-
rules << "@extend %darkIconFix !optional;" if icon['dark_icon_fix']
39-
items << "._icon-#{icon['type']}:before { #{rules.join(' ')} }"
37+
rules << "background-position: -#{item['col']}rem -#{item['row']}rem;"
38+
rules << "@extend %darkIconFix !optional;" if item['dark_icon_fix']
39+
items << "._icon-#{item['type']}:before { #{rules.join(' ')} }"
4040
end
4141

4242
items.join('')

lib/tasks/sprites.thor

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ class SpritesCLI < Thor
2121
bg_color = get_sidebar_background
2222

2323
items_with_icons.each_with_index do |item, index|
24-
if item[:has_icons]
25-
item[:row] = (index / icons_per_row).floor
26-
item[:col] = index - item[:row] * icons_per_row
24+
item[:row] = (index / icons_per_row).floor
25+
item[:col] = index - item[:row] * icons_per_row
2726

28-
item[:icon_16] = get_icon(item[:path_16], 16)
29-
item[:icon_32] = get_icon(item[:path_32], 32)
27+
item[:icon_16] = get_icon(item[:path_16], 16)
28+
item[:icon_32] = get_icon(item[:path_32], 32)
3029

31-
item[:dark_icon_fix] = needs_dark_icon_fix(item[:icon_32], bg_color)
32-
end
30+
item[:dark_icon_fix] = needs_dark_icon_fix(item[:icon_32], bg_color)
3331
end
3432

33+
log_details(items_with_icons, icons_per_row)
34+
3535
generate_spritesheet(16, items_with_icons, 'assets/images/sprites/docs.png') {|item| item[:icon_16]}
3636
generate_spritesheet(32, items_with_icons, 'assets/images/sprites/docs@2x.png') {|item| item[:icon_32]}
3737

3838
# Add Mongoose's icon details to docs without custom icons
39-
template_item = items_with_icons.find {|item| item[:type] == 'mongoose'}
39+
default_item = items_with_icons.find {|item| item[:type] == 'mongoose'}
4040
items_without_icons.each do |item|
41-
item[:row] = template_item[:row]
42-
item[:col] = template_item[:col]
43-
item[:dark_icon_fix] = template_item[:dark_icon_fix]
41+
item[:row] = default_item[:row]
42+
item[:col] = default_item[:col]
43+
item[:dark_icon_fix] = default_item[:dark_icon_fix]
4444
end
4545

4646
save_manifest(items, icons_per_row, 'assets/images/sprites/docs.json')
@@ -60,9 +60,9 @@ class SpritesCLI < Thor
6060

6161
# Checking paths against an array of possible paths is faster than 200+ File.exist? calls
6262
files = Dir.glob('public/icons/docs/**/*.png')
63-
items.map do |item|
63+
64+
items.each do |item|
6465
item[:has_icons] = files.include?(item[:path_16]) && files.include?(item[:path_32])
65-
item
6666
end
6767
end
6868

@@ -111,79 +111,79 @@ class SpritesCLI < Thor
111111
end
112112

113113
def get_luminance(color)
114-
rgba = [
114+
rgb = [
115115
ChunkyPNG::Color.r(color).to_f,
116116
ChunkyPNG::Color.g(color).to_f,
117-
ChunkyPNG::Color.b(color).to_f,
118-
ChunkyPNG::Color.a(color).to_f
117+
ChunkyPNG::Color.b(color).to_f
119118
]
120119

121-
rgba.map! do |rgb|
122-
rgb /= 255
123-
rgb < 0.03928 ? rgb / 12.92 : ((rgb + 0.055) / 1.055) ** 2.4
120+
rgb.map! do |value|
121+
value /= 255
122+
value < 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
124123
end
125124

126-
0.2126 * rgba[0] + 0.7152 * rgba[1] + 0.0722 * rgba[2]
125+
0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]
127126
end
128127

129-
def generate_spritesheet(size, icons, output_path, &icon_to_img)
128+
def generate_spritesheet(size, items_with_icons, output_path, &item_to_icon)
130129
logger.info("Generating spritesheet #{output_path} with icons of size #{size} x #{size}")
131130

132-
icons_per_row = Math.sqrt(icons.length).ceil
131+
icons_per_row = Math.sqrt(items_with_icons.length).ceil
133132
spritesheet = ChunkyPNG::Image.new(size * icons_per_row, size * icons_per_row)
134133

135-
icons.each do |icon|
136-
img = icon_to_img.call(icon)
134+
items_with_icons.each do |item|
135+
icon = item_to_icon.call(item)
137136

138137
# Calculate the base coordinates
139-
base_x = icon[:col] * size
140-
base_y = icon[:row] * size
138+
base_x = item[:col] * size
139+
base_y = item[:row] * size
141140

142141
# Center the icon if it's not a perfect rectangle
143-
x = base_x + ((size - img.width) / 2).floor
144-
y = base_y + ((size - img.height) / 2).floor
142+
x = base_x + ((size - icon.width) / 2).floor
143+
y = base_y + ((size - icon.height) / 2).floor
145144

146-
spritesheet.compose!(img, x, y)
145+
spritesheet.compose!(icon, x, y)
147146
end
148147

149148
FileUtils.mkdir_p(File.dirname(output_path))
150149
spritesheet.save(output_path)
151150
end
152151

153-
def save_manifest(icons, icons_per_row, path)
152+
def save_manifest(items, icons_per_row, path)
154153
logger.info("Saving spritesheet details to #{path}")
155154

156155
FileUtils.mkdir_p(File.dirname(path))
157156

158157
# Only save the details that the scss file needs
159-
manifest_icons = icons.map do |icon|
158+
manifest_items = items.map do |item|
160159
{
161-
:type => icon[:type],
162-
:row => icon[:row],
163-
:col => icon[:col],
164-
:dark_icon_fix => icon[:dark_icon_fix]
160+
:type => item[:type],
161+
:row => item[:row],
162+
:col => item[:col],
163+
:dark_icon_fix => item[:dark_icon_fix]
165164
}
166165
end
167166

168-
manifest = {:icons_per_row => icons_per_row, :icons => manifest_icons}
167+
manifest = {:icons_per_row => icons_per_row, :items => manifest_items}
169168

170169
File.open(path, 'w') do |f|
171170
f.write(JSON.generate(manifest))
172171
end
173172
end
174173

175-
def log_details(icons, icons_per_row)
176-
logger.debug("Amount of icons: #{icons.length}")
174+
def log_details(items_with_icons, icons_per_row)
175+
logger.debug("Amount of icons: #{items_with_icons.length}")
177176
logger.debug("Icons per row: #{icons_per_row}")
178177

179-
max_type_length = icons.map { |icon| icon[:type].length }.max
178+
max_type_length = items_with_icons.map {|item| item[:type].length}.max
180179
border = "+#{'-' * (max_type_length + 2)}+#{'-' * 5}+#{'-' * 8}+#{'-' * 15}+"
180+
181181
logger.debug(border)
182182
logger.debug("| #{'Type'.ljust(max_type_length)} | Row | Column | Dark icon fix |")
183183
logger.debug(border)
184184

185-
icons.each do |icon|
186-
logger.debug("| #{icon[:type].ljust(max_type_length)} | #{icon[:row].to_s.ljust(3)} | #{icon[:col].to_s.ljust(6)} | #{(icon[:dark_icon_fix] ? 'Yes' : 'No').ljust(13)} |")
185+
items_with_icons.each do |item|
186+
logger.debug("| #{item[:type].ljust(max_type_length)} | #{item[:row].to_s.ljust(3)} | #{item[:col].to_s.ljust(6)} | #{(item[:dark_icon_fix] ? 'Yes' : 'No').ljust(13)} |")
187187
end
188188

189189
logger.debug(border)
@@ -192,7 +192,7 @@ class SpritesCLI < Thor
192192
def logger
193193
@logger ||= Logger.new($stdout).tap do |logger|
194194
logger.level = options[:verbose] ? Logger::DEBUG : Logger::INFO
195-
logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
195+
logger.formatter = proc {|severity, datetime, progname, msg| "#{msg}\n"}
196196
end
197197
end
198198
end

0 commit comments

Comments
 (0)