[QUIZ][SOLUTION] Mexican Blanket (#127)

J

Jesse Edelstein

Here's a basic toolkit for drawing Mexican blankets in xterm.
Unfortunately, it *only* works in xterm and the like, and only in
those compiled with 256 color support, because the control codes are
so arcane. In fact, the code is quite simple - this quiz is really
more a matter of presentation, and I hope to find a nicer way to draw
this.

Some stuff to be improved on:
- Ugly (better if you turn the font size way down and make ARGV[0] large)
- Xterm-only
- The pattern is entirely hardcoded - I have to input all colors, the
order of dividers, etc. A more elgant solution would know something
about color fading and might be able to make some aesthetic choices on
its own. I really didn't want to have to implement that in xterm,
though!

def gradient(color_list)
old = [color_list[0]]
pattern = old*5
(1...color.length).each do |i|
new = [color_list]
1.upto(5) { |j| pattern += new*j + old*(5-j) }
old = new
end
return pattern
end

def divider(color)
[color.to_s]*5
end

def mexico
["28"]*4 + ["15"]*4 + ["88"]*4
end

# generate pattern
pattern = gradient(%w[16 22 28 34 40 46])
pattern += divider(0) #divider
pattern += gradient(%w[21 20 19 18 17 16])
pattern += mexico
pattern += gradient(%w[196 197 198 199 200 201])
pattern += divider(0)
pattern += gradient(%w[226 220 214 208 202 196])

# width of the flag from CLI
flagwidth = ARGV[0] ? ARGV[0].to_i : 80

# translate to xterm (256-color) control codes, and then print
pattern.collect! {|i| "\033[48;5;#{i}m "}
while (pattern.length >= flagwidth) do
puts pattern[0...flagwidth].join + "\033[0m"
pattern.slice!(0)
end
 
J

Jesse Edelstein

Here's a basic toolkit for drawing Mexican blankets in xterm.
Unfortunately, it *only* works in xterm and the like, and only in
those compiled with 256 color support, because the control codes are
so arcane. In fact, the code is quite simple - this quiz is really
more a matter of presentation, and I hope to find a nicer way to draw
this.

Some stuff to be improved on:
- Ugly (better if you turn the font size way down and make ARGV[0] large)
- Xterm-only
- The pattern is entirely hardcoded - I have to input all colors, the
order of dividers, etc. A more elgant solution would know something
about color fading and might be able to make some aesthetic choices on
its own. I really didn't want to have to implement that in xterm,
though!

Oops. Here's a corrected version that will actually run.

def gradient(color_list)
old = [color_list[0]]
pattern = old*5
(1...color_list.length).each do |i|
new = [color_list]
1.upto(5) { |j| pattern += new*j + old*(5-j) }
old = new
end
return pattern
end

def divider(color)
[color.to_s]*5
end

def mexico
["28"]*4 + ["15"]*4 + ["88"]*4
end

# generate pattern
pattern = gradient(%w[16 22 28 34 40 46])
pattern += divider(0) #divider
pattern += gradient(%w[21 20 19 18 17 16])
pattern += mexico
pattern += gradient(%w[196 197 198 199 200 201])
pattern += divider(0)
pattern += gradient(%w[226 220 214 208 202 196])

# width of the flag from CLI
flagwidth = ARGV[0] ? ARGV[0].to_i : 80

# translate to xterm (256-color) control codes, and then print
pattern.collect! {|i| "\033[48;5;#{i}m "}
while (pattern.length >= flagwidth) do
puts pattern[0...flagwidth].join + "\033[0m"
pattern.slice!(0)
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top