delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/04/08:49:56

Message-ID: <013501be384d$785ab3c0$a4023ace@alpha>
From: "Brian Bacon" <kyberteknik AT geocities DOT com>
To: <djgpp AT delorie DOT com>
Subject: Re: Changeing Colour Within A String
Date: Mon, 4 Jan 1999 17:48:17 -0800
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.1
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Reply-To: djgpp AT delorie DOT com

This is a multi-part message in MIME format.

------=_NextPart_000_0132_01BE380A.68D88000
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I've done this before, and I just got done trying again... I don't know if
it needs ANSI.SYS (I doubt it, it uses conio's functions), however, there
are a few problems... :)   First, it doesn't accept normal backslash codes
yet (including newline!)  But I can iron that out, or you can do it.
Second, there is no proverbial {x.  It is a bit more complicated to keep
track of the current color than to set it (I'm sure its not that hard
though).  On a good note, it provides both formatted and non-formatted text
printing and escape codes to set only the foreground, only the background,
or both.  AND... you can just change the defines to alter the escape codes
and color codes.  Oh, and another thing; this is not the best implementation
I've done before, but if you don't like it you can probably use it to figure
out a better way.

Attached is the source, the header, and an example source.

-Brian

p.s. the example program should help ALOT to figure out how to use it :)
and it uses a quick fix for the escape code problem.. a define that printf's
a \n :)

-----Original Message-----
From: Kieran Farrell <kezman AT bigfoot DOT com>
Newsgroups: comp.os.msdos.djgpp
To: djgpp AT delorie DOT com <djgpp AT delorie DOT com>
Date: Monday, January 04, 1999 2:25 AM
Subject: Changeing Colour Within A String


>Hi guys,
>
>I was wondering if someone could give me some ideas on how to change
>text colour within a string. The easiest way is to explain what I want
>is to give an example.
>
>#include <conio.h>
>
>int main(void)
>{
>   sprintf( buf,"{wHello {Y There{x" );
>   /*********************************************************
>     * where {w turns the text after it white *
>     * where {Y turns the following text Bright Yellow *
>     * where {x ends the colour code *
>     *********************************************************/
>   cputs( buf );
>   return 0;
>}
>
>so you get a dull white hell and a bright Yellow There. the {x means
>that the colour code is reset to normal for the next cputs cprintf
>etc. hmm hard to explain I guess but if I leave the {x out I want the
>next cputs or cprintf to be the last colour used, so in the code
>example above minus the {x and after the cputs add a cprintf(
>"glunk"); the glunk will be bright yellow still.
>
>Can this be done, I have see it work on a mud and would like to use it
>in my programs. I haven't access to the mud code to have a look at how
>they did it. Ack sorry for rambling
>
>Thanx in advance
>
>Kieran Farrell

------=_NextPart_000_0132_01BE380A.68D88000
Content-Type: application/octet-stream;
	name="clrprint.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="clrprint.c"

/* clrprint.c - (c) 1998 Brian Bacon, All rights reserved.      */
/*  - released for all to use                                   */
/*  - only restriction: don't remove my copyright :)            */
/*                                                              */
/* Usage notes:                                                 */
/*                                                              */
/*  Edit the define values below to fit your needs              */
/*                                                              */
/*  clrprint can accept escape codes to set the foreground      */
/*  color, the background color, or both.  When using both      */
/*  the background code comes before the foreground code.       */
/*                                                              */
/*  clrprint doesn't handle normal escape codes (the            */
/*  backslash codes).  I'm working on it.                       */
/*                                                              */
/*  This source is EASILY editable to fit your needs, hell      */
/*  you can even add in codes to clear the screen, or display   */
/*  the time & date (I've done this & more before).             */
/*                                                              */
/*  Check the example program for a more complete example :)    */

#include <conio.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

/* change to suit needs */
#define _CP_FG_ESC_CODE '!'     /* signals foreground change */
#define _CP_BG_ESC_CODE '#'     /* signals background change */
#define _CP_ESC_CODE    '@'     /* signals both fg and bg change */

/* you can change these values too.. */
/* NOTICE!  I use 'grey' conio uses 'gray' */
#define _CP_BLACK       '0'
#define _CP_BLUE        '1'
#define _CP_GREEN       '2'
#define _CP_CYAN        '3'
#define _CP_RED         '4'
#define _CP_MAGENTA     '5'
#define _CP_BROWN       '6'
#define _CP_LIGHTGREY   '7'
#define _CP_DARKGREY    '8'     /* when using these values for the */
#define _CP_LIGHTBLUE   '9'     /* background, they produce their */
#define _CP_LIGHTGREEN  'A'     /* dark counterparts with blinking */
#define _CP_LIGHTCYAN   'B'     /* foreground text, unless blinking */
#define _CP_LIGHTRED    'C'     /* is turned off by a conio call to */
#define _CP_LIGHTMAGENTA 'D'    /* intensevideo() */
#define _CP_YELLOW      'E'
#define _CP_WHITE       'F'

/* converts a single character (one of the above) to an attribute number */
#define _CP_GET_ATTR(x)                                         \
        switch (x) {                                            \
                case _CP_BLACK:         x=BLACK;        break;  \
                case _CP_BLUE:          x=BLUE;         break;  \
                case _CP_GREEN:         x=GREEN;        break;  \
                case _CP_CYAN:          x=CYAN;         break;  \
                case _CP_RED:           x=RED;          break;  \
                case _CP_MAGENTA:       x=MAGENTA;      break;  \
                case _CP_BROWN:         x=BROWN;        break;  \
                case _CP_LIGHTGREY:     x=LIGHTGRAY;    break;  \
                case _CP_DARKGREY:      x=DARKGRAY;     break;  \
                case _CP_LIGHTBLUE:     x=LIGHTBLUE;    break;  \
                case _CP_LIGHTGREEN:    x=LIGHTGREEN;   break;  \
                case _CP_LIGHTCYAN:     x=LIGHTCYAN;    break;  \
                case _CP_LIGHTRED:      x=LIGHTRED;     break;  \
                case _CP_LIGHTMAGENTA:  x=LIGHTMAGENTA; break;  \
                case _CP_YELLOW:        x=YELLOW;       break;  \
                case _CP_WHITE:         x=WHITE;        break;  \
                default:                x=-1;           break;  \
        }                                                       \

/* print formatted text with color codes */
int clrprintf(char *format, ...)
{
  char          buf[256];
  va_list       ap;

  va_start(ap, format);
  vsprintf(buf, format, ap);
  va_end(ap);

  return(clrprint(buf));
}

/* print color coded text */
/* returns total number of characters printed */
/* does NOT handle normal escape codes! (it will...) */
int clrprint(char *buf)
{
  int           spos,           /* current position in buf */
                slen,           /* length of buf */
                ccount;         /* count of characters printed */
  char          newfg,          /* new fg color */
                newbg;          /* new bg color */

  slen=strlen(buf);

  for (spos=0; spos<slen; spos++) {
      if ((buf[spos] == _CP_FG_ESC_CODE) && (spos<=slen-1)) {
         newfg=buf[spos+1];
         _CP_GET_ATTR(newfg);
         if (newfg>=0) {
           textcolor(newfg);    /* set foreground */
           spos+=2;             /* skip esc & color codes */
         }         
      } else if ((buf[spos] == _CP_BG_ESC_CODE) && (spos<=slen-1)) {
        newbg=buf[spos+1];
        _CP_GET_ATTR(newbg);
        if (newbg>=0) {
           textbackground(newbg);       /* set background */
           spos+=2;             /* skip esc & color codes */
         }         
      } else if ((buf[spos] == _CP_ESC_CODE) && (spos<=slen-2)) {
        newbg=buf[spos+1];
        newfg=buf[spos+2];
        _CP_GET_ATTR(newbg);
        _CP_GET_ATTR(newfg);
        if ((newbg>=0) && (newfg>=0)) {
           textattr((newbg<<4) | newfg);
           spos+=3;             /* skip esc & both color codes */
        }
      }
      ccount++;
      putch(buf[spos]);
  }

  return(ccount);
}

------=_NextPart_000_0132_01BE380A.68D88000
Content-Type: application/octet-stream;
	name="clrprint.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="clrprint.h"

/* clrprint.c - (c) 1998 Brian Bacon, All rights reserved.      */
/*  - released for all to use                                   */
/*  - only restriction: don't remove my copyright :)            */

#ifndef _CP_HEADER
#define _CP_HEADER

int clrprintf(char *format, ...);
int clrprint(char *buf);

#endif

------=_NextPart_000_0132_01BE380A.68D88000
Content-Type: application/octet-stream;
	name="example.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="example.c"

#include "clrprint.h"

#define newline         printf("\n");

int main(void)
{
  clrprintf("!FI just set the foreground to white!"); newline;
  clrprintf("#1Background now blue w/white fore:)"); newline;
  clrprintf("@2EYellow on green? !FWhite? #4on Red? @07Am I ##0%d?", 1); =
newline;
  clrprintf("Notice I had to use two #'s so it would print the first"); =
newline;
  clrprintf("You don't like it?  Then change the !3c!4o!5d!6e!7s!9!"); =
newline;
  return 1;
}

------=_NextPart_000_0132_01BE380A.68D88000--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019