color on console

Randall L. Smith randy at rls.UUCP
Sun Feb 25 17:57:48 AEST 1990


In article <9mB5e3w160w at nstar.uucp>, larry at nstar.uucp (Larry Snyder) writes:
> Does anyone have a utility for use under 386/ix that sets
> the color on the console like the SCO "setcolor" utility?

Here's one inspired by SCO setcolor I whipped up awhile back.  It's
not very well written nor has lots-o-features but you get the source
and may help you.  Enjoy.

/*
 *  color.c - Set color of monitor.
 *
 *  Author: Randall L. Smith, October 1988.
 *  Flames: rls!randy at tut.cis.ohio-state.edu -or- ...!osu-cis!rls!randy
 *
 *  This code is hereby transferred to the public domain.  Make big money
 *  or anything else you like.	Even pretend you wrote it because I don't
 *  want to admit this is mine. :-)  Some pretty obvious improvements can
 *  be made, but it just doesn't matter to me right now.
 *
 *  This has been compiled under MSC 4.0.
 *
 *   Param	Desc
 *	8	Invisible (black on black) display.  Yeesh.
 *     30	Foreground black
 *     31	Foreground red
 *     32	Foreground green
 *     33	Foreground yellow
 *     34	Foreground blue
 *     35	Foreground magenta
 *     36	Foreground cyan
 *     37	Foreground white
 *     40	Background black
 *     41	Background red
 *     42	Background green
 *     43	Background yellow
 *     44	Background blue
 *     45	Background magenta
 *     46	Background cyan
 *     47	Background white
 *
 *   Bugs:  Has stupid notion that the default color of screen is green.
 *	    Aren't they all? :-)
 *
 */

#include <stdio.h>

char colors;
int fprintf();
int printf();
int strcmp();

main(argc, argv)
int argc;
char *argv[];

{
    if (argc == 3) {
	if ((strcmp(argv[1], "-F") == 0) || (strcmp(argv[1], "-f") == 0))
	    colors = '3';
	else if ((strcmp(argv[1], "-B") == 0) || (strcmp(argv[1], "-b") == 0))
	    colors = '4';
    } else {
	(void) print_help();
	return -1;
    }

    if (strcmp(argv[2], "black") == 0) {
	(void) printf("\033[%c0m", colors);	/*  black   */
    } else if (strcmp(argv[2], "red") == 0) {
	(void) printf("\033[%c1m", colors);	/*  red     */
    } else if (strcmp(argv[2], "green") == 0) {
	(void) printf("\033[%c2m", colors);	/*  green   */
    } else if (strcmp(argv[2], "yellow") == 0) {
	(void) printf("\033[1;%c3m", colors);	/*  yellow  */
    } else if (strcmp(argv[2], "blue") == 0) {
	(void) printf("\033[%c4m", colors);	/*  blue    */
    } else if (strcmp(argv[2], "magenta") == 0) {
	(void) printf("\033[%c5m", colors);	/*  magenta */
    } else if (strcmp(argv[2], "cyan") == 0) {
	(void) printf("\033[%c6m", colors);	/*  cyan    */
    } else if (strcmp(argv[2], "white") == 0) {
	(void) printf("\033[%c7m", colors);	/*  white   */
    } else {
	(void) fprintf(stderr, "Unknown color %s \n", argv[2]);
	(void) print_help();
	return -1;
    }
    return 0;
}

/*
 * Default black background - green foreground.
 */

print_help()
{
   (void) fprintf(stderr, "Usage: color -[fbh] desired-color \n");
   (void) fprintf(stderr, "Valid colors are: ");
   (void) fprintf(stderr, " \033[47;30m black \033[40;31m red \033[32m green \033[1;33m yellow\n");
   (void) fprintf(stderr, "		   \033[0;34m blue \033[35m magenta \033[36m cyan \033[37m white\n");
   (void) fprintf(stderr, " \033[32m for [f]oreground or [b]ackground.\n");
   return 0;
}



More information about the Comp.unix.i386 mailing list