Skip to content

Instantly share code, notes, and snippets.

@nickrobson
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickrobson/ace0f9de4c18c801dc13 to your computer and use it in GitHub Desktop.
Save nickrobson/ace0f9de4c18c801dc13 to your computer and use it in GitHub Desktop.
colours!
//
// colours.h
// COMP1917
//
// Created by Nick Robson on 20/03/2015.
// Copyright (c) 2015 Nick Robson. All rights reserved.
//
#ifndef __COMP1917__colours__
#define __COMP1917__colours__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#define SETUP_CONSOLE HANDLE hConsole = \
GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO \
consoleInfo; WORD saved_attributes; \
GetConsoleScreenBufferInfo(hConsole, &consoleInfo); \
saved_attributes = consoleInfo.wAttributes;
#define TEXT_ATTR(attr) SetConsoleTextAttribute(hConsole, (attr));
#define SET_COLOR_FG(r, g, b, i) TEXT_ATTR(((r) ? FOREGROUND_RED : 0)\
+ ((g) ? FOREGROUND_GREEN : 0) + ((b) ? FOREGROUND_BLUE : 0) + ((i) ?\
FOREGROUND_INTENSITY : 0))
#define SET_COLOR_BG(r, g, b, i) TEXT_ATTR(((r) ? BACKGROUND_RED : 0)\
+ ((g) ? BACKGROUND_GREEN : 0) + ((b) ? BACKGROUND_BLUE : 0) + ((i) ?\
BACKGROUND_INTENSITY : 0))
#define SET_COLOR_FG_BG(r, g, b, i, r2, g2, b2, i2) TEXT_ATTR(((r) ?\
FOREGROUND_RED : 0) + ((g) ? FOREGROUND_GREEN : 0) + ((b) ?\
FOREGROUND_BLUE : 0) + ((i) ? FOREGROUND_INTENSITY : 0) + ((r2) ?\
BACKGROUND_RED : 0) + ((g2) ? BACKGROUND_GREEN : 0) + ((b2) ?\
BACKGROUND_BLUE : 0) + ((i2) ? BACKGROUND_INTENSITY : 0))
#define UPDATE SET_COLOR_FG_BG(textRed, textGreen, \
textBlue, textHighlight, backRed, backGreen, backBlue, backHighlight)
#define SET_FG_DARKRED SET_COLOR_FG(1, 0, 0, 0)
#define SET_FG_RED SET_COLOR_FG(1, 0, 0, 1)
#define SET_FG_DARKBLUE SET_COLOR_FG(0, 0, 1, 0)
#define SET_FG_BLUE SET_COLOR_FG(0, 0, 1, 1)
#define SET_FG_DARKGREEN SET_COLOR_FG(0, 1, 0, 0)
#define SET_FG_GREEN SET_COLOR_FG(0, 1, 0, 1)
#define SET_FG_GOLD SET_COLOR_FG(1, 1, 0, 0)
#define SET_FG_YELLOW SET_COLOR_FG(1, 1, 0, 1)
#define SET_FG_PURPLE SET_COLOR_FG(1, 0, 1, 0)
#define SET_FG_PINK SET_COLOR_FG(1, 0, 1, 1)
#define SET_FG_DARKCYAN SET_COLOR_FG(0, 1, 1, 0)
#define SET_FG_CYAN SET_COLOR_FG(0, 1, 1, 1)
#define SET_FG_GRAY SET_COLOR_FG(1, 1, 1, 0)
#define SET_FG_DARKGRAY SET_COLOR_FG(0, 0, 0, 1)
#define SET_FG_BLACK SET_COLOR_FG(0, 0, 0, 0)
#define SET_FG_WHITE SET_COLOR_FG(1, 1, 1, 1)
#define SET_BG_DARKRED SET_COLOR_BG(1, 0, 0, 0)
#define SET_BG_RED SET_COLOR_BG(1, 0, 0, 1)
#define SET_BG_DARKBLUE SET_COLOR_BG(0, 0, 1, 0)
#define SET_BG_BLUE SET_COLOR_BG(0, 0, 1, 1)
#define SET_BG_DARKGREEN SET_COLOR_BG(0, 1, 0, 0)
#define SET_BG_GREEN SET_COLOR_BG(0, 1, 0, 1)
#define SET_BG_GOLD SET_COLOR_BG(1, 1, 0, 0)
#define SET_BG_YELLOW SET_COLOR_BG(1, 1, 0, 1)
#define SET_BG_PURPLE SET_COLOR_BG(1, 0, 1, 0)
#define SET_BG_PINK SET_COLOR_BG(1, 0, 1, 1)
#define SET_BG_DARKCYAN SET_COLOR_BG(0, 1, 1, 0)
#define SET_BG_CYAN SET_COLOR_BG(0, 1, 1, 1)
#define SET_BG_GRAY SET_COLOR_BG(1, 1, 1, 0)
#define SET_BG_DARKGRAY SET_COLOR_BG(0, 0, 0, 1)
#define SET_BG_BLACK SET_COLOR_BG(0, 0, 0, 0)
#define SET_BG_WHITE SET_COLOR_BG(1, 1, 1, 1)
#define RESET_COLORS TEXT_ATTR(saved_attributes)
#define RESET RESET_COLOURS
#define CLEAR_SCREEN system("cls");
#else
#define SETUP_CONSOLE
#define SET_COLOR_FG(ansi) printf("\033[%sm", (ansi));
#define SET_COLOR_BG(ansi) printf("\033[%sm", (ansi));
#define SET_COLOR_FG_BG(a, an) printf("\033[%s;%sm", (a), (an));
#define SET_FG_DARKRED SET_COLOR_FG("31")
#define SET_FG_RED SET_COLOR_FG("31")
#define SET_FG_DARKGREEN SET_COLOR_FG("32")
#define SET_FG_GREEN SET_COLOR_FG("32")
#define SET_FG_DARKBLUE SET_COLOR_FG("34")
#define SET_FG_BLUE SET_COLOR_FG("34")
#define SET_FG_GOLD SET_COLOR_FG("33")
#define SET_FG_YELLOW SET_COLOR_FG("33")
#define SET_FG_PURPLE SET_COLOR_FG("35")
#define SET_FG_PINK SET_COLOR_FG("35")
#define SET_FG_DARKCYAN SET_COLOR_FG("36")
#define SET_FG_CYAN SET_COLOR_FG("36")
#define SET_FG_GRAY SET_COLOR_FG("37")
#define SET_FG_WHITE SET_COLOR_FG("37")
#define SET_FG_DARKGRAY SET_COLOR_BG("30")
#define SET_FG_BLACK SET_COLOR_BG("30")
#define SET_BG_DARKRED SET_COLOR_BG("41")
#define SET_BG_RED SET_COLOR_BG("41")
#define SET_BG_DARKGREEN SET_COLOR_BG("42")
#define SET_BG_GREEN SET_COLOR_BG("42")
#define SET_BG_DARKBLUE SET_COLOR_BG("44")
#define SET_BG_BLUE SET_COLOR_BG("44")
#define SET_BG_GOLD SET_COLOR_BG("43")
#define SET_BG_YELLOW SET_COLOR_BG("43")
#define SET_BG_PURPLE SET_COLOR_BG("45")
#define SET_BG_PINK SET_COLOR_BG("45")
#define SET_BG_DARKCYAN SET_COLOR_BG("46")
#define SET_BG_CYAN SET_COLOR_BG("46")
#define SET_BG_GRAY SET_COLOR_BG("47")
#define SET_BG_WHITE SET_COLOR_BG("47")
#define SET_BG_DARKGRAY SET_COLOR_BG("40")
#define SET_BG_BLACK SET_COLOR_BG("40")
#define UPDATE fg = 0; bg = 0; \
if (textRed && textBlue && textGreen) { fg = 37; } \
else if (textRed && textBlue) { fg = 35; } \
else if (textRed && textGreen) { fg = 33; } \
else if (textBlue && textGreen) { fg = 36; } \
else if (textRed) { fg = 31; } \
else if (textBlue) { fg = 34; } \
else if (textGreen) { fg = 32; } \
else { fg = 30; } \
if (backRed && backBlue && backGreen) { bg = 47; } \
else if (backRed && backBlue) { bg = 45; } \
else if (backRed && backGreen) { bg = 44; } \
else if (backBlue && backGreen) { bg = 46; } \
else if (backRed) { bg = 41; } \
else if (backBlue) { bg = 44; } \
else if (backGreen) { bg = 42; } \
else { bg = 40; } \
printf("\033[%d;%dm", fg, bg);
#define RESET_COLORS printf("\033[0m");
#define CLEAR_SCREEN system("clear");
#endif
// variables!
#define SETUP_VARS int textRed = 0; int textGreen = 0; \
int textBlue = 0; int textHighlight = 0; int backRed = 0; \
int backGreen = 0; int backBlue = 0; int backHighlight = 0; \
int fg = 0; int bg = 0;
#define TOGGLE_FG_RED textRed = !textRed;
#define TOGGLE_FG_GREEN textGreen = !textGreen;
#define TOGGLE_FG_BLUE textBlue = !textBlue;
#define TOGGLE_FG_HIGHLIGHT textHighlight = !textHighlight;
#define TOGGLE_BG_RED backRed = !backRed;
#define TOGGLE_BG_GREEN backGreen = !backGreen;
#define TOGGLE_BG_BLUE backBlue = !backBlue;
#define TOGGLE_BG_HIGHLIGHT backHighlight = !backHighlight;
#define isRedText() textRed
#define isGreenText() textGreen
#define isBlueText() textBlue
#define isHighlightText() textHighlight
#define isRedBack() backRed
#define isGreenBack() backGreen
#define isBlueBack() backBlue
#define isHighlightBack() backHighlight
#define USE_VARS textRed = 0; textGreen = 0; \
textBlue = 0; textHighlight = 0; backRed = 0; \
backGreen = 0; backBlue = 0; backHighlight = 0; fg = 0; bg = 0;
// backwards compatibility
#define SETUP SETUP_VARS SETUP_CONSOLE USE_VARS
#define RESET RESET_COLORS
#define CLEAR CLEAR_SCREEN
#endif
//
// colours.c
// COMP1917
//
// Created by Nick Robson on 20/03/2015.
// Copyright (c) 2015 Nick Robson. All rights reserved.
//
#include "colours.h"
int main(int argc, char* argv[]) {
SETUP
SET_FG_RED printf("red\n");
SET_FG_DARKRED printf("darkred\n");
SET_FG_BLUE printf("blue\n");
SET_FG_DARKBLUE printf("darkblue\n");
SET_FG_GREEN printf("green\n");
SET_FG_DARKGREEN printf("darkgreen\n");
SET_FG_YELLOW printf("yellow\n");
SET_FG_GOLD printf("gold\n");
SET_FG_PINK printf("pink\n");
SET_FG_PURPLE printf("purple\n");
SET_FG_CYAN printf("cyan\n");
SET_FG_DARKCYAN printf("darkcyan\n");
SET_FG_WHITE printf("white\n");
SET_FG_GRAY printf("gray\n");
RESET
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment