fix: color change is now working

This commit is contained in:
0x35c
2025-11-03 18:32:20 +01:00
parent a0185690cb
commit 86ce44deff
5 changed files with 87 additions and 67 deletions

44
headers/color.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <stdint.h>
struct color {
char *name;
uint32_t value;
};
#define BLACK 0x000000
#define BLUE 0x0000FF
#define GREEN 0x008000
#define CYAN 0x00FFFF
#define RED 0xFF0000
#define MAGENTA 0xFF00FF
#define BROWN 0xA52A2A
#define LIGHT_GREY 0xD3D3D3
#define DARK_GREY 0x555555
#define LIGHT_BLUE 0xADD8E6
#define LIGHT_GREEN 0x90EE90
#define LIGHT_CYAN 0xE0FFFF
#define LIGHT_RED 0xFF6666
#define LIGHT_MAGENTA 0xFF77FF
#define LIGHT_YELLOW 0xFFFF01
#define WHITE 0xFFFFFF
static const struct color colors[] = {
{"BLACK", BLACK},
{"BLUE", BLUE},
{"GREEN", GREEN},
{"CYAN", CYAN},
{"RED", RED},
{"MAGENTA", MAGENTA},
{"BROWN", BROWN},
{"LIGHT_GREY", LIGHT_GREY},
{"DARK_GREY", DARK_GREY},
{"LIGHT_BLUE", LIGHT_BLUE},
{"LIGHT_GREEN", LIGHT_GREEN},
{"LIGHT_CYAN", LIGHT_CYAN},
{"LIGHT_RED", LIGHT_RED},
{"LIGHT_MAGENTA", LIGHT_MAGENTA},
{"LIGHT_YELLOW", LIGHT_YELLOW},
{"WHITE", WHITE},
};

View File

@ -22,7 +22,6 @@ struct screen {
uint32_t fg_color;
uint32_t bg_color;
uint8_t buffer[VGA_WIDTH * VGA_HEIGHT];
uint32_t default_color;
struct icon *background;
struct font *font;
char line[256];
@ -52,6 +51,8 @@ enum cursor_direction { LEFT, RIGHT, UP, DOWN };
void terminal_initialize(void);
void terminal_set_bg_color(uint32_t color);
void terminal_set_fg_color(uint32_t color);
uint32_t terminal_get_fg_color(void);
uint32_t terminal_get_bg_color(void);
int terminal_putchar(char c);
int terminal_write(const char *data, size_t size);
int terminal_writestring(const char *data);
@ -61,11 +62,7 @@ void terminal_clear(void);
struct key_event terminal_getkey(void);
void update_cursor(void);
void move_cursor(int direction);
void set_color_level(int level);
void terminal_set_default_fg_color(uint32_t fg_color);
void terminal_set_default_bg_color(uint32_t fg_color);
void terminal_change_default_fg_color(uint32_t color);
uint32_t terminal_get_default_color(void);
uint8_t terminal_get_char(int column, int row);
void terminal_remove_last_char(void);
void terminal_refresh_color(void);
// TODO void terminal_refresh_color(void);

View File

@ -1,4 +1,5 @@
#include "base.h"
#include "color.h"
#include "ctype.h"
#include "terminal.h"
@ -7,6 +8,15 @@
int print_int_base(int64_t number, const char *prefix, const char *base);
static void set_color_level(int level)
{
static uint32_t levels[] = {
BLACK, RED, RED, MAGENTA, LIGHT_YELLOW,
LIGHT_YELLOW, LIGHT_BLUE, GREEN, LIGHT_GREY,
};
terminal_set_fg_color(levels[level]);
}
static int get_level(const char *str)
{
if (!str || !isdigit(str[0]))
@ -43,9 +53,11 @@ int kvprintf(const char *restrict format, va_list *ap)
int ret = 0;
const int level = get_level(format);
set_color_level(level);
if (level)
uint32_t old_color = terminal_get_fg_color();
if (level) {
set_color_level(level);
start++;
}
while (*start != '\0') {
if (*start == '%' && *(start + 1) != '\0') {
ret += print_flag(*(start + 1), ap);
@ -55,7 +67,7 @@ int kvprintf(const char *restrict format, va_list *ap)
}
start++;
}
set_color_level(0);
terminal_set_fg_color(old_color);
update_cursor();
return ret;
}

View File

@ -1,47 +1,31 @@
#include "color.h"
#include "kprintf.h"
#include "string.h"
#include "terminal.h"
#include "utils.h"
#include <stdint.h>
struct color {
char *name;
int value;
};
void color_cmd(char *arg)
{
uint8_t tmp_color;
static const struct color colors[] = {
{"BLACK", 0x000000}, {"BLUE", 0x0000FF},
{"GREEN", 0x008000}, {"CYAN", 0x00FFFF},
{"RED", 0xFF0000}, {"MAGENTA", 0xFF00FF},
{"BROWN", 0xA52A2A}, {"LIGHT_GREY", 0xD3D3D3},
{"DARK_GREY", 0x555555}, {"LIGHT_BLUE", 0xADD8E6},
{"LIGHT_GREEN", 0x90EE90}, {"LIGHT_CYAN", 0xE0FFFF},
{"LIGHT_RED", 0xFF6666}, {"LIGHT_MAGENTA", 0xFF77FF},
{"LIGHT_YELLOW", 0xFFFFE0}, {"WHITE", 0xFFFFFF}};
if (!arg)
goto invalid;
for (size_t i = 0; i < ARRAY_SIZE(colors); i++) {
if (strcmp(colors[i].name, arg) == 0) {
terminal_set_fg_color(colors[i].value);
kprintf("color: %s\n", colors[i].name);
// terminal_refresh_color();
return;
}
}
invalid:
tmp_color = terminal_get_default_color();
uint32_t tmp_color = terminal_get_fg_color();
kprintf(KERN_WARNING "Invalid argument\n");
kprintf("Available colors: ");
for (size_t i = 0; i < ARRAY_SIZE(colors); i++) {
terminal_set_default_fg_color(colors[i].value);
terminal_set_fg_color(colors[i].value);
kprintf(colors[i].name);
if (i != ARRAY_SIZE(colors) - 1)
kprintf(", ");
}
terminal_set_default_fg_color(tmp_color);
terminal_set_fg_color(tmp_color);
kprintf("\n");
}

View File

@ -26,8 +26,7 @@ void terminal_initialize(void)
for (int i = 0; i < TERM_COUNT; i++) {
screens[i].row = 0;
screens[i].column = 0;
screens[i].default_color = 0;
screens[i].fg_color = screens[i].default_color;
screens[i].fg_color = 0;
screens[i].bg_color = 0x8acd01;
// screens[i].background = &image_icon;
screens[i].font = consolas_regular_13_font;
@ -86,22 +85,22 @@ void terminal_set_fg_color(uint32_t fg_color)
screen->fg_color = fg_color;
}
uint32_t terminal_get_bg_color(void)
{
return screen->bg_color;
}
uint32_t terminal_get_fg_color(void)
{
return screen->fg_color;
}
void terminal_set_color(uint32_t fg_color, uint32_t bg_color)
{
screen->fg_color = fg_color;
screen->bg_color = bg_color;
}
void terminal_set_default_fg_color(uint32_t fg_color)
{
screen->default_color = fg_color;
}
uint32_t terminal_get_default_color(void)
{
return screen->default_color;
}
uint8_t terminal_get_char(int x, int y)
{
return screen->buffer[y * VGA_WIDTH + x];
@ -145,15 +144,16 @@ static void terminal_new_line(void)
terminal_scroll();
}
void terminal_refresh_color(void)
{
// terminal_clear();
for (uint32_t y = 0; y < VGA_HEIGHT; y++)
for (uint32_t x = 0; x < VGA_WIDTH; x++)
terminal_putentryat(
get_font_node(screen->buffer[y * VGA_WIDTH + x]),
screen->fg_color, x, y);
}
// TODO
// void terminal_refresh_color(void)
// {
// // terminal_clear();
// for (uint32_t y = 0; y < VGA_HEIGHT; y++)
// for (uint32_t x = 0; x < VGA_WIDTH; x++)
// terminal_putentryat(
// get_font_node(screen->buffer[y * VGA_WIDTH + x]),
// screen->fg_color, x, y);
// }
void terminal_clear(void)
{
@ -235,20 +235,3 @@ void move_cursor(int direction)
}
update_cursor();
}
void set_color_level(int level)
{
int color_translation[] = {
VGA_COLOR_WHITE, VGA_COLOR_RED,
VGA_COLOR_RED, VGA_COLOR_MAGENTA,
VGA_COLOR_LIGHT_YELLOW, VGA_COLOR_LIGHT_YELLOW,
VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN,
VGA_COLOR_LIGHT_GREY,
};
if (level == 0) {
terminal_set_color(screen->default_color, screen->bg_color);
return;
}
terminal_set_fg_color(color_translation[level]);
}