Painting - RealDoigt/ConsolePaint GitHub Wiki
Painting
is a static class which offers several basic drawing functions as well as a few advanced ones.
This class is not very useful to a programmer looking for mostly prebaked stuff, however most of the functions in the library depend on this class; it is the foundation upon which the rest of the library stands.
brush
is a public char field which can be modified freely. That char is what will be printed on the screen when you call the basic drawing methods and the ones which use them. It's initial value is the block character '█'.
Here is an example of accessing brush
in C#:
using ConsolePaint;
public static void Main()
{
// Notice how the field name is not capitalized. Painting.Brush will not work!
Painting.brush = 'a';
}
DrawCell
is a public method which has two unsigned 16-bit integers (short) and one ConsoleColor as parameters; posX
, posY
and color
.
It prints a single brush
character at the given X and Y position with the provided colour. The method makes a backup of the current foreground colour, so you don't need to worry about changing back to the previous colour. However, the cursor will have its position changed.
DrawCell
will crash the program in these cases:
- If either
posX
orposY
are negative values, the method will throw aNegativeCoordinateException
. - If either
posX
orposY
are greater thanConsole.WindowWidth
orConsole.WindowHeight
respectively, the method will throw anOutOfWindowBoundsException
.
Here is an example of using DrawCell
in C#:
using ConsolePaint;
public static void Main()
{
// This will print a red █ at (x: 12; y: 4) on the screen and the cursor's Left value will increase to 13.
Painting.DrawCell(12, 4, ConsoleColor.Red);
}
DrawHorizontalLine
is a public method which has three unsigned 16-bit integers (short) and one ConsoleColor as parameters; posX
, posY
, length
and color
. The method will print a horizontal line of brush
characters to the right of the position (posX
, posY
). The length and colour of the line are determined by the length
and color
parameters, respectively. The method makes a backup of the current foreground colour, so you don't need to worry about changing back to the previous colour. However, the cursor will have its position changed.
DrawHorizontalLine
will crash the program in these cases:
- If
length
is a negative value, the method will throw aNegativeLengthException
. - If either
posX
orposY
are negative values, the method will throw aNegativeCoordinateException
. - If either
posX
+length
orposY
are greater thanConsole.WindowWidth
orConsole.WindowHeight
respectively, the method will throw anOutOfWindowBoundsException
.
Here is an example of using DrawHorizontalLine
in C#:
using ConsolePaint;
public static void Main()
{
/*
This will print 5 dark blue █ at (x: 10; y: 0) on the screen
and the cursor's Left value will increase to 15.
*/
Painting.DrawHorizontalLine(10, 0, 5, ConsoleColor.DarkBlue);
}
DrawVerticalLine
is a public method which has three unsigned 16-bit integers (short) and one ConsoleColor as parameters; posX
, posY
, length
and color
. The method will print a vertical line of brush
characters down from the position (posX
, posY
). The length and colour of the line are determined by the length
and color
parameters, respectively. The method makes a backup of the current foreground colour, so you don't need to worry about changing back to the previous colour. However, the cursor will have its position changed.
DrawVerticalLine
will crash the program in these cases:
- If
length
is a negative value, the method will throw aNegativeLengthException
. - If either
posX
orposY
are negative values, the method will throw aNegativeCoordinateException
. - If either
posY
+length
orposX
are greater thanConsole.WindowHeight
orConsole.WindowWidth
respectively, the method will throw anOutOfWindowBoundsException
.
Here is an example of using DrawHorizontalLine
in C#:
using ConsolePaint;
public static void Main()
{
/*
This will print 5 dark blue █ at (x: 10; y: 0) on the screen
and the cursor's top value will increase to 5.
*/
Painting.DrawVerticalLine(10, 0, 5, ConsoleColor.DarkBlue);
}
DrawDiagonalLine
is a public method which has three unsigned 16-bit integers (short), one ConsoleColor and one signed 8-bit integer (sbyte) as parameters; posX
, posY
, length
, color
and angle
. The method will print a diagonal line of brush
characters to the right of the position (posX
, posY
). It then either goes up if angle
is negative or down if angle
is positive. Do note that angle
here is not a real unit of anything; It is arbitrarily called an angle, but it actually works as a multiplier. The length and colour of the line are determined by the length
and color
parameters, respectively. The method makes a backup of the current foreground colour, so you don't need to worry about changing back to the previous colour. However, the cursor will have its position changed.
DrawDiagonalLine
will crash the program in these cases:
- If the absolute value of
angle
is not 1, the method will throw anInvalidAngleException
. - If
length
is a negative value, the method will throw aNegativeLengthException
. - If either
posX
orposY
are negative values, the method will throw aNegativeCoordinateException
. - If the final character of the line would be out of the console boundary, the method will throw an
OutOfWindowBoundsException
.
Here is an example of using DrawDiagonalLine
in C#:
using ConsolePaint;
public static void Main()
{
const byte X = 5, Y = 10, LENGTH = 5;
// these ones go up
Painting.DrawDiagonalLine(X, Y, LENGTH, ConsoleColor.Green, -1);
Painting.DrawDiagonalLine(X + LENGTH, Y + LENGTH, LENGTH, ConsoleColor.Green, -1);
// these ones go down
Painting.DrawDiagonalLine(X, Y + 1, LENGTH, ConsoleColor.DarkGreen, 1);
Painting.DrawDiagonalLine(X + LENGTH, Y - LENGTH + 1, LENGTH, ConsoleColor.DarkGreen, 1);
// The lines give shape to a diamond/lozenge
}
DrawSpinningAnimation
is a public method which has a func<bool> and an unsigned 32-bit integer (int) as parameters; expr
and millisecondDelay
.
The method animates a spinning line at the current cursor location using the various slash and dash characters. This method's purpose is to show some kind of visible activity while the program is otherwise occupied doing something else. expr
is expected to be a function which returns true if the animation should continue and false if it should end. millisecondDelay
controls the speed of the animation; the lower the number is, the faster the animation is. If the value of millisecondDelay
is lower than 0, the animation will not get anymore faster.
Here is an example of using DrawSpinningAnimation
in C#:
using ConsolePaint;
public static void Main()
{
Func<bool> isGoingToSpinForever = () => true;
Painting.DrawSpinningAnimation(isGoingToSpinForever, 0);
// this will effectively display the animation forever at max speed.
}
The methods which follow deal with displaying images in the terminal window. Due to the limitations of the terminal when using .Net, some things may not go as smoothly as expected. For example, small images will appear larger in the console since individual pixels are not used to draw the picture, instead, characters are used. Characters tend to be twice as high as their width, so the image will most likely look distorted. Also, you can't predict how large the console's window size will be or which font your users will be using; those are indirect parameters which WILL affect the look of your application. Finally, draw speeds vary between updates and OS; Windows will generally be slow to draw, while Linux may be faster than expected.
There are techniques you can use to work around these problems, which are discussed in their own article.
DrawImage
is a public method which has a bitmap and two unsigned 16-bit integers (short) as parameters; image
, posX
and posY
.
DrawImage
doesn't really do anything on its own; it itself is an overload of the actual method which will convert image
to the limited 16 colours of the .Net console, then draws the resulting picture on the console window using the brush
character. That picture will be drawn at the position(posX
, posY
). Be careful with the image size and position, you can easily go out of bounds with relatively small images; you may have to resize the image before displaying it in the console using this method.
From calling this method, the program may crash:
- If either
posX
orposY
are negative values, aNegativeCoordinateException
will be thrown. - If the image would be out of bounds, a
OutOfWindowBoundsException
will be thrown.
Here is an example of using DrawImage
in C#:
using ConsolePaint;
public static void Main()
{
Painting.DrawImage(new System.Drawing.Bitmap("rainbow wave.png"), 0, 0);
// The method will draw this picture: https://i.imgur.com/OR0pV9h.png
}
DrawDarkImage
works the same way as DrawImage
, but the filter used is more tolerant and darker, which improves the contrast in very bright images which don't render too well on normal brightness. However, that usually comes at the cost of accuracy and details. For example, run the above example using DrawDarkImage
instead, you will notice that the image looks much better, but it is also much darker:
The pros and cons of both methods are best seen in the Rick Roll in console experiments:
Close up shots and zooms such as the first frames on Rick's face look better with the normal method, but the high contrast of the darker method allows the viewer to more clearly identify what is happening in the later frames of the video where the brightness would be too high.
Draw
is a private method which has two unsigned 16-bit integers (short) parameters; posX
and posY
.
Draw
prints the brush
character at the screen position of posX
and posY
. This also has the effect of changing the cursor's position.
This method can only be used from within the Painting
class as it does no window bound checks nor negative value checks, so use it carefully.
If you plan contributing to ConsolePaint and if you have to use the Draw
method, you will be expected to write code which handles these aforementioned checks.
Example code:
def myDef(x as short, y as short):
if x < Console.WindowWidth and x > -1 and y < Console.WindowHeight and y > -1:
Draw(x, y)