mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-04 16:46:08 +02:00
Implement chat rendering and message handling with colored segments
This commit is contained in:
@@ -18,6 +18,9 @@ public class ImageToAsciiService
|
||||
|
||||
var sb = new StringBuilder();
|
||||
|
||||
byte lastR = 0, lastG = 0, lastB = 0;
|
||||
bool hasLastColor = false;
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
@@ -25,11 +28,26 @@ public class ImageToAsciiService
|
||||
var pixel = image[x, y];
|
||||
var brightness = 0.299 * pixel.R + 0.587 * pixel.G + 0.114 * pixel.B;
|
||||
|
||||
// Map brightness (0-255) to ASCII char index (inverted: dark pixels get dense chars)
|
||||
// Map brightness (0-255) to ASCII char index
|
||||
var index = (int)((brightness / 255.0) * (AsciiChars.Length - 1));
|
||||
|
||||
// Emit ANSI 24-bit color only when it changes
|
||||
if (!hasLastColor || pixel.R != lastR || pixel.G != lastG || pixel.B != lastB)
|
||||
{
|
||||
sb.Append($"\x1b[38;2;{pixel.R};{pixel.G};{pixel.B}m");
|
||||
lastR = pixel.R;
|
||||
lastG = pixel.G;
|
||||
lastB = pixel.B;
|
||||
hasLastColor = true;
|
||||
}
|
||||
|
||||
sb.Append(AsciiChars[index]);
|
||||
}
|
||||
|
||||
// Reset color at end of line
|
||||
sb.Append("\x1b[0m");
|
||||
hasLastColor = false;
|
||||
|
||||
if (y < image.Height - 1)
|
||||
{
|
||||
sb.AppendLine();
|
||||
|
||||
Reference in New Issue
Block a user