The Accidental Rubyist

invalid byte sequence in UTF-8

256 colors with ruby-ncurses

with 7 comments

My ruby-ncurses says that I have 256 colors, (the constant Ncurses.COLORS). Using some ruby scripts that use escape sequences, I am able to print 256 colors. Does anyone know how to access these 256 colors using ruby-ncurses?
(I obviously do have xterm-256color and screen-256color working on my system for ruby to say 256).

Using an escape sequence in the print command of a window is printing the escape sequences, whereas using the same escape sequence (taken from Highline, for underline), in *irb* is correctly printing underline in irb.

EDIT: I got a reply from the  very helpful maintainer of ncurses (Thomas Dickey) on 256 colors. He says ncurses must be compiled with “ext” option. Also, one can refer to the colors by numbers 1..256. Without recompiling, I wrote a loop and printed colors and saw a few shades of blue. But most other colors looked like they were repeating. I am in no hurry for 256 colors, but thought I would share this.

Advertisements

Written by totalrecall

November 30, 2008 at 7:31 pm

Posted in ncurses, ruby

7 Responses

Subscribe to comments with RSS.

  1. Could you paste your example code? I’ve been trying for a while to get 256 colors working in Ruby ncurses without any luck.

    Peter H

    January 23, 2009 at 12:18 am

  2. Seems I made some scratch file – cant find it …

    But it had a simple loop with a pause for a key after every 20 or 30 lines.
    Just as you normally create colors using init_pair, do so, but use numbers instead of COLOR_XXX. Those are integers, anyway.

    Use numbers as your arguments for init_pair.
    When you print “Ncurses.COLORS” in your program, it should show 256. It does so if I set TERM=256screen or 256xterm etc.

    However, it is necessary for your ncurses to be compiled as I told you in the post.

    If you want 16 colors (using combinations of BOLd and REVERSE, you can check a program named colormap.rb in my github (lib/colormap.rb). You will also need lib/ver/window) which is also there.
    Run it from command line as ruby lib/ver/colormap.
    Press keys 0-9 etc as advised.

    totalrecall

    January 23, 2009 at 1:22 am

  3. Sorry it is lib/rbcurse/colormap.

    totalrecall

    January 23, 2009 at 1:24 am

  4. Here is a program I did find on the net: but it uses xterm codes from ruby. Perhaps you have een it already:

    #!/usr/bin/env ruby

    class ColorDemo

    ColorString = “##”
    ColorMode = {‘fg’ => ’38’, ‘bg’ => ’48’}

    def show_welcome
    puts <<-END
    +—————————————————————————-+
    | This program shows 256-color support in xterm-compliant terminals. You may |
    | notice some flickering while the color codes are mapped in your terminal. |
    | Please resize your terminal to at least the width of this box. Try this: |
    | xterm -fg beige -bg midnightblue -fa "antialias=true:rgba=0:pixelsize=18" |
    +————-+————————————————————–+
    | Color Range | Description |
    +————-+————————————————————–+
    | 000 – 015 | Standard ANSI colors, but with more pleasing shades |
    | 016 – 231 | 6x6x6 color cube |
    | 232 – 255 | Greyscale ramp, with black and white intentionally omitted |
    +————-+————————————————————–|
    | Translation: 256colors.rb by Daniel Butler 2005-10-30 |
    | Original: 256colors2.pl by Todd Larason v1.1 1999-07-11 |
    +—————————————————————————-+
    END
    end

    def clear_screen
    system(“clear”)
    end

    def loop_color_cube(mode = nil)
    (0…6).each do |red|
    (0…6).each do |green|
    (0…6).each do |blue|
    yield red, green, blue
    end
    print nocolor(” “) if mode == :cube
    end
    print “\n” if mode == :cube
    end
    end

    def setup_color_cube_color
    # Colors 16-231 are a 6x6x6 color cube
    loop_color_cube do |red, green, blue|
    printf(“\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\”,
    16 + (red * 36) + (green * 6) + blue,
    (red * 42.5).to_i,
    (green * 42.5).to_i,
    (blue * 42.5).to_i)
    end
    end

    def setup_color_cube_gray
    # Colors 232-255 are a grayscale ramp, intentionally
    # leaving out black and white
    (0…24).each do |gray|
    level = (gray * 10) + 8
    printf(“\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\”,
    232 + gray, level, level, level);
    end
    end

    def colorize(color, text, mode)
    “\x1b[#{ColorMode[mode]};5;#{color}m#{text}”
    end

    def nocolor(text)
    “\x1b[0m#{text}”
    end

    def display_system_colors(mode)
    puts “System colors (#{mode}):”
    (0…16).each do |color|
    print colorize(color, ColorString, mode)
    print nocolor(“\n”) if [7, 15].include? color
    end
    end

    def display_color_cube(mode)
    puts “Color cube, 6x6x6 (#{mode}):”
    loop_color_cube(:cube) do |red, green, blue|
    print colorize(16 + (red * 36) +
    (green * 6) + blue, ColorString, mode)
    end
    end

    def display_grayscale_ramp(mode)
    puts “Grayscale ramp (#{mode}):”
    (232…256).each do |color|
    print colorize(color, ColorString, mode)
    end
    print nocolor(“\n”)
    end

    def go
    clear_screen
    show_welcome
    setup_color_cube_color
    setup_color_cube_gray
    [‘fg’, ‘bg’].each do |mode|
    display_system_colors(mode)
    display_color_cube(mode)
    display_grayscale_ramp(mode)
    end
    end
    end

    demo = ColorDemo.new
    demo.go

    totalrecall

    January 23, 2009 at 1:26 am

  5. Yes, I’ve set up my terminal for 256 colors (vim looks really nice) and seen that Ruby script. Using your suggestion, I came up with this script, that should print all the combinations of foreground and background colors. Instead, it only has one 256-color run and a whole lot of blinking:

    I saw from your other posts you’re new to GitHub — one neat thing about Gist is that you can fork them and edit them, so if you have any suggestions how to fix this program you can do it right there.

    I’d really appreciate any help, I’ve spent a lot of time trying to make this work without any luck.

    Peter H

    January 23, 2009 at 1:56 am

  6. Some observations:
    1. You are printing onto a window well beyond its bounds. So it will pretend to go on printing but wont.
    We must pause and clear the screen after a screenful or so.

    2. Keep the BG constant for a loop and cycle the fg.

    I have messed up your code so it can print: i notice that on my mc using INIT_COLOR beyond 256 does now work ,perhaps since i have not compiled ext.

    So i began cycling the color pair number after 256 also.

    I am stiill trying to locate the earlier program i wrote. That was giving a better output.

    totalrecall

    January 23, 2009 at 12:02 pm

  7. 1. No, I just made my terminal 256×256 or so. It’s easy if you size the font down.
    2. Hm, very interesting: this is the same basic effect I got: I could display all 256 colors as background or as foreground, but not together.

    Peter H

    January 24, 2009 at 10:23 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: