Skip to content

Instantly share code, notes, and snippets.

@pahaz pahaz/Redactor.cs
Last active Aug 29, 2015

Embed
What would you like to do?
Simple image redactor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Redactor
{
static void Main(string[] args)
{
Console.Title = "Simple image redactor";
// drawPalette();
Console.SetCursorPosition(0, 50);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Clear();
int x = 20;
int y = 20;
int draw_color = 4;
int back_color = 1;
bool writeMode = false;
int old_color = back_color, old_y = y, old_x = x;
int[][] img = makeFill(79, 25, 1);
do
{
// update image
if (writeMode)
{
img[0][0] = 14;
}
else
{
img[0][0] = 10;
}
img[x][y] = draw_color;
// draw
Console.Clear();
draw(img);
Console.SetCursorPosition(0, 0);
// restore
if (writeMode) { }
else
{
img[old_x][old_y] = old_color;
}
// listen event
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.LeftArrow)
{
x -= 1;
}
else if (keyPressed.Key == ConsoleKey.RightArrow)
{
x += 1;
}
else if (keyPressed.Key == ConsoleKey.UpArrow)
{
y -= 1;
}
else if (keyPressed.Key == ConsoleKey.DownArrow)
{
y += 1;
}
else if (keyPressed.Key == ConsoleKey.Spacebar)
{
writeMode = !writeMode;
}
// store data
old_color = img[x][y];
old_x = x;
old_y = y;
} while (true);
Console.ReadKey();
}
static void drawPalette()
{
Console.WriteLine(" == PALETTE == ");
for (int i = 0; i < 16; i++)
{
if (i < 10) { Console.Write(" "); }
else { Console.Write(" "); }
Console.Write(i);
Console.Write(":");
Console.Write(intToChar(i));
Console.WriteLine(":");
Console.WriteLine();
}
}
static int[][] makeFill(int width, int height, int color)
{
int[][] img = new int[width][];
for (int i = 0; i < width; i++)
{
img[i] = new int[height];
for (int j = 0; j < height; j++)
{
img[i][j] = color;
}
}
return img;
}
static void draw(int[][] img)
{
int width = img.Length;
int height = img[0].Length;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
Console.Write(intToChar(img[i][j]));
}
Console.WriteLine();
}
}
static char intToChar(int i)
{
char c = (char)0;
if (i == 00)
{
c = ' ';
}
else if (i == 01)
{
c = '░';
}
else if (i == 02)
{
c = '▒';
}
else if (i == 03)
{
c = '▓';
}
else if (i == 04)
{
c = '█';
}
else if (i == 05)
{
c = '│';
}
else if (i == 06)
{
c = '┌';
}
else if (i == 07)
{
c = '┐';
}
else if (i == 08)
{
c = '└';
}
else if (i == 09)
{
c = '┘';
}
else if (i == 10)
{
c = '─';
}
else if (i == 11)
{
c = '┴';
}
else if (i == 12)
{
c = '┬';
}
else if (i == 13)
{
c = '├';
}
else if (i == 14)
{
c = '┼';
}
else if (i == 15)
{
c = '┤';
}
/*
0 1 2 3 4 5 6 7 8 9 A B C D E F
B0 ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐
C0 └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧
D0 ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀
*/
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.