Skip to content

Instantly share code, notes, and snippets.

@Alfonzzoj
Last active June 15, 2024 18:13
Show Gist options
  • Save Alfonzzoj/db207b89d56f24d9d0b17ff93e091be8 to your computer and use it in GitHub Desktop.
Save Alfonzzoj/db207b89d56f24d9d0b17ff93e091be8 to your computer and use it in GitHub Desktop.
Libreria de colores C / C++

Libreria de colores para C / C++ 🎨

Permite modificar el color de fuente del texto y color de fondo en consola al momento de ejecucion.

Screenshot-2

fondos

( font color and background color )


Comenzando 🚀

Estas instrucciones te permitirán obtener una copia del proyecto en funcionamiento en tu máquina local para propósitos de desarrollo y pruebas.

Pre-requisitos 📋

Dependiendo del lenguaje que se utilice se necesita una libreria u otra

  • En C
#include <stdio.h>
  • En C++
#include <iostream>

Instalación 🔧

  1. Descargar el zip o el archivo libreria "colors.h"
  2. Incluir el archivo colors.h en el directorio de tu programa / proyecto
  3. Importarla desde tu archivo C ó C++ con:
#include "colors.h"

Despliegue 📦

La libreria se basa en codigos de colores a traves de ANSI Escape codes

Color Font code Background code
Black \x1B[30m\x1B[40m
Red \x1B[31m \x1B[41m
Green \x1B[32m \x1B[42m
Yellow \x1B[33m \x1B[43m
Blue \x1B[34m \x1B[44m
Magenta \x1B[35m \x1B[45m
Cyan \x1B[36m \x1B[46m
White \x1B[37m \x1B[47m
Cualquier color (con V en [0-255]) \x1B[38;5;Vm \x1B[48;5;Vm
Cualquier RGB color (Con valores en [0-255]) \x1B[38;2;R;G;Bm \x1B[48;2;R;G;Bm

Siendo 38 el numero para colores personalizados


Colores disponibles
Name Code
Color Font Background
Rojo RED BG_RED
Verde claro LGREEN BG_LGREEN
Verde GREEN BG_GREEN
Amarillo YELLOW BG_YELLOW
Cyan CYAN BG_CYAN
Azul claro LBLUE BG_LBLUE
Azul BLUE BG_BLUE
Pink ROSE BG_ROSE
Magenta MAGENTA BG_MAGENTA
Blanco WHITE BG_WHITE
Naranja ORANGE BG_ORANGE
Gris GRAY BG_GRAY
Negro BLACK BG_BLACK

Wiki 📖

Para cambiar el color de la fuente se es necesario concatenar el Font code de su respectivo color, con el mensaje a mostrar.

  • En C
#include <stdio.h>
#include "colors.h"

int main () {
 
  printf( RED      " This text is RED!     \n" );
  printf( LGREEN   " This text is LGREEN!  \n" );
  printf( GREEN    " This text is GREEN!   \n" );
  printf( YELLOW   " This text is YELLOW!  \n" );
  printf( BLUE     " This text is BLUE!    \n" );
  printf( ORANGE   " This text is ORANGE   \n" );
  printf( MAGENTA  " This text is MAGENTA! \n" );
 
  return 0;
}
  • En C++
#include <iostream>
#include "colors.h"

using namespace std;

int main () {
 
  cout << RED     << " This text is RED!     " << endl;
  cout << LGREEN  << " This text is LGREEN!  " << endl;
  cout << GREEN   << " This text is GREEN!   " << endl;
  cout << YELLOW  << " This text is YELLOW!  " << endl;
  cout << BLUE    << " This text is BLUE!    " << endl;
  cout << ORANGE  << " This text is ORANGE   " << endl;
  cout << MAGENTA << " This text is MAGENTA! " << endl;
 
  return 0;
}

Salida:

Screenshot-2


Para cambiar el color de fondo se necesita la clave RESET al final de la instruccion.

  1. Se concatena al inicio el Background_color a utilizar
  2. Se finaliza el mensaje con el codigo RESET
#include <iostream>
#include "colors.h"

using namespace std;

int main () {
  cout << RED     <<BG_YELLOW << "This text is RED!      " << RESET << endl;
  cout << LGREEN  <<BG_CYAN   << "This text is LGREEN!   " << RESET << endl;
  cout << GREEN   <<BG_RED    << "This text is GREEN!    " << RESET << endl;
  cout << YELLOW  <<BG_GREEN  << "This text is YELLOW!   " << RESET << endl;
  cout << CYAN    <<BG_MAGENTA<< "This text is CYAN!     " << RESET << endl;
  cout << BLUE    <<BG_LGREEN << "This text is BLUE!     " << RESET << endl;
  cout << ORANGE  <<BG_BLUE   << "This text is ORANGE!   " << RESET << endl;
  cout << MAGENTA <<BG_ORANGE << "This text is MAGENTA!  " << RESET << endl;
 
  return 0;
}

Salida:

fondos

⚠ ❗ Precaucion ❗ ⚠

No tomar en cuenta la sentencia RESET puede provocar que el background_color se corra a la siguiente linea o al fin de la consola.

Colabora 💭

Estan abiertas las colaboraciones para mas colores personalizados.

Expresiones de Gratitud 🎁

  • Comenta a otros sobre este proyecto 📢.
  • No olvides dejarme una ⭐.
  • Da las gracias públicamente 🤓.
//===Color font code===/
#define BLACK "\x1B[30m"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define WHITE "\x1B[37m"
#define ORANGE "\x1B[38;2;255;128;0m"
#define ROSE "\x1B[38;2;255;151;203m"
#define LBLUE "\x1B[38;2;53;149;240m"
#define LGREEN "\x1B[38;2;17;245;120m"
#define GRAY "\x1B[38;2;176;174;174m"
#define RESET "\x1b[0m"
//===Color background code===/
#define BG_BLACK "\x1B[40m"
#define BG_RED "\x1B[41m"
#define BG_GREEN "\x1B[42m"
#define BG_YELLOW "\x1B[43m"
#define BG_BLUE "\x1B[44m"
#define BG_MAGENTA "\x1B[45m"
#define BG_CYAN "\x1B[46m"
#define BG_WHITE "\x1B[47m"
#define BG_ORANGE "\x1B[48;2;255;128;0m"
#define BG_LBLUE "\x1B[48;2;53;149;240m"
#define BG_LGREEN "\x1B[48;2;17;245;120m"
#define BG_GRAY "\x1B[48;2;176;174;174m"
#define BG_ROSE "\x1B[48;2;255;151;203m"
@gus2131
Copy link

gus2131 commented Jun 10, 2024

Muchas gracias, lo estoy usando en códigos de la universidad <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment