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

**URL:** <https://www.ruby-forum.com/t/solution-mexican-blanket-127/106452>\
**Category:** Ruby\
**Created:** [June 10, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/solution-mexican-blanket-127/106452 "2007-06-10T18:59:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesseek12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/0ea827/32.png) [@jesseek12](https://www.ruby-forum.com/u/jesseek12)\
**Post date:** [June 10, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/solution-mexican-blanket-127/106452/1 "2007-06-10T18:59:41Z")

</div>

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[i]]  
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

---

<div class="post-metadata">

**Author:** ![jesseek12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/0ea827/32.png) [@jesseek12](https://www.ruby-forum.com/u/jesseek12)\
**Post date:** [June 10, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/solution-mexican-blanket-127/106452/2 "2007-06-10T20:02:50Z")

</div>

On 6/10/07, Jesse E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - 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[i]]  
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
