Ncurses colours without allocating colour pairs?

This is a discussion on Ncurses colours without allocating colour pairs? within the C Programming forums, part of the General Programming Boards category; Is there a way to use colours in ncurses without having to declare the colour pair? I ask because I ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Ncurses colours without allocating colour pairs?

    Is there a way to use colours in ncurses without having to declare the colour pair? I ask because I need to have 128 different combinations and surely declaring all those is a bad idea. Thanks

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I don't see any way in the man pages to do that. What are you doing that you need 128 different combinations? Perhaps you could write a small program to generate the code necessary for that. And start_color() gives you 8 colors without having to do much, so it's like you only need 120 :P

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by joshdick
    I don't see any way in the man pages to do that. What are you doing that you need 128 different combinations? Perhaps you could write a small program to generate the code necessary for that. And start_color() gives you 8 colors without having to do much, so it's like you only need 120 :P
    Well I'm making a program to load a zzt world file and display it like zzt would. Where I go from there I'm not sure

    I can't think of a way to make the function though, the zzt colour code thing is similar but not the same as ncurses's colours.

    Say if I defined the basic colours like this:

    Code:
    #define BLACK 0
    #define DARK_BLUE 1
    #define DARK_GREEN 2
    #define DARK_CYAN 3
    #define DARK_RED 4
    #define DARK_PURPLE 5
    #define DARK_YELLOW 6
    #define LIGHT_GREY 7
    #define DARK_GREY 8
    #define LIGHT_BLUE 9
    #define LIGHT_GREEN 10
    #define LIGHT_CYAN 11
    #define LIGHT_RED 12
    #define LIGHT_PURPLE 13
    #define LIGHT_YELLOW 14
    #define WHITE 15
    Then I could declare colour pairs for each cobinations:

    Code:
    init_pair ( BLACK_BLACK, BLACK, BLACK );
    init_pair ( DARK_BLUE_BLACK, DARK_BLUE, BLACK);
    /* .... */
    But I need to think of a clever way to get from BLACK BLACK to BLACK_BLACK. Its all a bit confusing !

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    To find out what foreground color and background color is used by a color pair, use the function int pair_content(short pair, short *f, short *b). To find out the definition of a color use the function int color_content(short color, short *r, short *g, short *b)

    From http://www.apmaths.uwo.ca/~xli/ncurses.html

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by joshdick
    To find out what foreground color and background color is used by a color pair, use the function int pair_content(short pair, short *f, short *b). To find out the definition of a color use the function int color_content(short color, short *r, short *g, short *b)

    From http://www.apmaths.uwo.ca/~xli/ncurses.html
    Do you think a simple loop through all the pairs checking for the right colours would be really inefficient? If I could do that I could just allocate all the pairs at the start of the program and then make a little function that takes two colours and returns the right pair's number.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I think that sounds efficient and downright swell.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    114
    Russled up some code . Not totaly sorted yet but you get the idea Thanks

    Code:
    int pair_count = 0;
    
    /* Colours */
    #define BLACK 0
    #define DARK_BLUE 1
    #define DARK_GREEN 2
    #define DARK_CYAN 3
    #define DARK_RED 4
    #define DARK_PURPLE 5
    #define DARK_YELLOW 6
    #define LIGHT_GREY 7
    #define DARK_GREY 8
    #define LIGHT_BLUE 9
    #define LIGHT_GREEN 10
    #define LIGHT_CYAN 11
    #define LIGHT_RED 12
    #define LIGHT_PURPLE 13
    #define LIGHT_YELLOW 14
    #define WHITE 15
    
    void init()
    {
    	/* Initialise colours */
    	short i, j;
    	
    	for (i = 0; i < 8; i++)
            for (j = 0; j < 16; j++)
            { 
    	       init_pair ( pair_count, j, i);
               pair_count++;
            }   
    }
    
    short pair(short wanted_fore, int wanted_back)
    {
        int i;
        short fore, back;
        
        for (i = 0; i < pair_count; i++)
        {
            pair_content(i, &fore, &back);
            if ( (fore == wanted_fore) & (back == wanted_back) )
                return i;
        }
    }  
    
    /* Being used */
    attron (COLOR_PAIR(pair(LIGHT_RED,LIGHT_YELLOW)));
    printw("TEST");

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    114
    It was all going so well and then I had a few problems with wierd colours showing up. After a while of chucking print's everywhere I have just figured out that theres a limit of 64 colour pairs! ARGH! Now I'm totaly buggered

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you tried upping the number for COLOR_PAIR? If you can't, then you need to build a "palette". Set up constants for all of the pairs. Now define all of your items to have a set color pair value. (For example, you needed 128 color sets, thus, all of your items should have a value of 0 - 128 defining what color set they use.) Now then, when building a scene, you need to just make sure that you don't have more than 64 different colors selected. You can change your "palette" between scenes by making a list of what colors are already in the palette that are to be reused, and then replacing any of the ones you aren't going to be using this time through.

    Read this, paying attention to the pharagraph starting with: "The init_pair routine changes the definition of a color-pair."

    You're not buggered. You just need to rethink it a bit.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by quzah
    Have you tried upping the number for COLOR_PAIR? If you can't, then you need to build a "palette". Set up constants for all of the pairs. Now define all of your items to have a set color pair value. (For example, you needed 128 color sets, thus, all of your items should have a value of 0 - 128 defining what color set they use.) Now then, when building a scene, you need to just make sure that you don't have more than 64 different colors selected. You can change your "palette" between scenes by making a list of what colors are already in the palette that are to be reused, and then replacing any of the ones you aren't going to be using this time through.

    Read this, paying attention to the pharagraph starting with: "The init_pair routine changes the definition of a color-pair."

    You're not buggered. You just need to rethink it a bit.

    Quzah.
    I have read that link lots but I really can't see how to increase the color_pair limit on it.

    The idea of just using the colours you need is clever, I could always not initilise any, and when my pairs function is called it searches for the colour pair, if it can't be found it makes it / throws a colour limit reached error.

    The thing is I'm sure normaly 64 colour pairs is pleanty but its kind of a shame having a limit incase someones map has more than that on it.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    114
    Well I got it working-ish. (Although it made me see that I'm not getting the player's colour properly!)

    Code:
    short pair(short wanted_fore, short wanted_back)
    {
        int i;
        short fore, back;
        
        if ( (wanted_fore > 16) | (wanted_back > 8) | (wanted_fore < 0) | (wanted_back < 0) )
        {
            perror("Colour pair wanted is out of range");
            return 0;
        }    
        
        for (i = 0; i < pair_count; i++)
        {
            pair_content(i, &fore, &back);
            if ( (fore == wanted_fore) & (back == wanted_back) )
                return i;
        }
        
        if (pair_count >= 64)
        {
            perror("Colour pair limit reached");
            return 0;
        }
        else
        {    
            init_pair ( pair_count, wanted_fore, wanted_back);
            pair_count++;
        
            return pair_count - 1;
        }    
    }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     if ( (wanted_fore > 16) | (wanted_back > 8) | (wanted_fore < 0) | (wanted_back < 0) )
        {
            perror("Colour pair wanted is out of range");
            return 0;
        }
    Just in case you aren't aware, you're using a bit-wise OR there. Did you mean to? You would be better served using the logical-OR there due to short circuiting of the if check.

    It doesn't save you a lot, but it is faster there because in the even the first check is true, it will stop testing the rest of the check. Where as with with the bit-wise or, it still tests all of the checks, and ORs the results together to give you one final true / false.

    You do the same thing later on with a bit-wise AND. You want || and && for logical tests.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    114
    ok learn somthing new every day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Curses-- colour pairs
    By sufthingol in forum C++ Programming
    Replies: 0
    Last Post: 03-21-2005, 08:26 PM
  4. DirectDraw colour matching
    By Hunter2 in forum Game Programming
    Replies: 2
    Last Post: 12-10-2002, 02:17 PM
  5. Colour theory... (weird title)
    By Magos in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2001, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21