Thread: Change color per Character ncurses

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    21

    Question Change color per Character ncurses

    Hello,
    I am writing a program that reads a file and prints each character of a file in different randomly chosen color. I am using ncurses for that,
    But the problem is that i cannot change color per character basis, rather it changes color of full screen each time while refreshing instead of character,
    Code:
    #include<ncurses.h>
    #include<math.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int row, col, x, y, main_Color, r, g, b, c;
        char ch;
        
        FILE *fp;
    
        if(argc != 2)
        {
            printf("Usage : %s <file>", argv[0]);
            exit(1);
        }
    
        fp = fopen(argv[1], "r");
            
        if(fp == NULL)
        {
            perror("\nCannot open file\n");    
            exit(1);
        }
    
        initscr();
        getmaxyx(stdscr, row, col);
        start_color();
        c = 1;
    
        while((ch = fgetc(fp)) != EOF)
        {
            getyx(stdscr, y, x);
    
            if(y == (row - 1))
            {
                printw("<-- Press any key -->");
                getch();
                clear();
                move(0,0);
            }
    
            main_Color = rand()%8;
            r = rand()%1001;
            g = rand()%1001;
            b = rand()%1001;
    
            init_color(main_Color, r,g,b);
            
            //c is for random color pair index...
            init_pair(c, main_Color, COLOR_BLACK);
            chgat(1, A_NORMAL ,c, NULL);
            printw("%c", ch);
            refresh();
            c++;
        }
    
        refresh();
        getch();
    
        fclose(fp);
        endwin();
    }
    Am i suppose to use something else than chgat, also im not getting black background....

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    EOF is an int, so ch needs to be an int (confusing, I know).

    I can't help you with ncurses, but I can show you how to do this with conio.h. Can you use conio.h, instead of ncurses?

    What exactly are you trying to make with the char's being printed?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    I know eof is int, it's just macro for int value, what im trying to achieve that i get every character of different(random) color..

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, one more time. ch should be an int.

    Can you use conio.h, instead of ncurses?

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    Actually im following a tutorial of ncurses, not sure about conio.h, all i now it's clrscr() and getchar()(guess im right)..

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "all i now" ??

    My fingers are dying to keyboard "Speak'em inglesh, mooch?", but don't worry - I won't let them.

    Keep working on that tutorial. Someone may come along and know the answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i change backround color and text color?
    By Nathan the noob in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2009, 04:39 AM
  2. Change RichTextBox Color
    By C_ntua in forum C# Programming
    Replies: 8
    Last Post: 12-18-2008, 03:44 PM
  3. If the RGB color is (64, 64, 255), change it to (64, 255, 64).
    By Grayson_Peddie in forum C# Programming
    Replies: 2
    Last Post: 06-14-2003, 04:26 PM
  4. OpenGL Color Change?
    By drdroid in forum Game Programming
    Replies: 1
    Last Post: 03-02-2003, 04:29 PM
  5. Can I change background color
    By Bryan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 12:14 PM

Tags for this Thread