
Processing, 2017
A program written with Processing which allows the user to draw a mandala. Free to use.
























Code:
class colourPicker {
int x, y, w, h;
color colour, c;
colourPicker(int _x, int _y, int _width, int _height){
x = _x;
y = _y;
w = _width;
h = _height;
}
void drawTable() {
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
int b = 100;
colorMode(HSB, w, 100, 100); //change to HSB mode
fill(i, j, b); // change colour depending on position of pixel
stroke(i, j, b);
rect(x+i, y+j, 1, 1); //draw colour table
}
}
}
void colourSelect() {
if(mousePressed){
if(mouseX > x && mouseY > y && mouseX < x + w && mouseY < y + h){
colour = get(mouseX, mouseY); //select colour on mousepressed if it is within the colour table
fill(0);
stroke(0);
rect(x, y + h, w, 70); //draw background behind colour select section
fill(colour); //set colour to selected colour
stroke(colour);
rect(x, y + h + 10, 50, 50); //show selected colour
textAlign(TOP);
text("#" + hex(colour, 6), x + 60, y + h + 20); //show hex value
text(round(red(colour)) + ", " + round(green(colour)) + ", " + round(blue(colour)), x + 60, y + h + 40); //show rgb value
text(colour, x + 60, y + h + 60); //show hsb value
}
}
c = get(x+25, y+h+25); //get the selected colour to use
}
}
class Drawer {
int x, y;
Drawer(){
}
void colour(color c){ //set colour of drawer
fill(c);
stroke(c);
}
void draw(int _x, int _y){
if(mousePressed == true){
if(mouseButton == LEFT){
ellipse(_x, _y, 1, 1); //draw on left mouse click
}
}
}
void clear(){
if(mousePressed == true){
if(mouseButton == RIGHT){
background(0); //clear on right mouse click
}
}
}
}
class Mandala {
float numSegments, segmentAngle;
int x, y, w, h;
int newX, newY;
Drawer pen;
Mandala(int _numSegments){
numSegments = _numSegments; //how many segments the mandala has
segmentAngle = TWO_PI / numSegments;//calculate segment angle depending on number of segments
pen = new Drawer(); //integrate drawing program
}
void createCanvas(int _x, int _y, int _w, int _h){
x = _x;
y = _y;
w = _w;
h = _h;
newX = x + w / 2;
newY = y + h / 2;
translate(newX, newY); //translate origin to center of canvas
}
void draw(color c){
for(int i = 0; i < numSegments; i++){ //repeat for each segment
pushMatrix();
float rA = segmentAngle * i; //calculate rotation angle from segmentAngle and the number of the segment being drawn
rotate(rA); //rotate
if(numSegments > 6 && numSegments % 2 == 0 && i % 2 != 0){
scale(-1, 1); //mirror every other segment only if there are more than 6 segments and an even amount of segments
}
//object to be rotated should go here:
if(mouseX > x && mouseY > y && mouseX < x + w && mouseY < y + h){
pen.colour(c);
pen.draw(mouseX - newX, mouseY - newY); //Translates in opposite direction of previous translation so drawing follows the mouse
pen.clear();
}
popMatrix();
}
}
}
Mandala m;
colourPicker colPicker;
void setup(){
size(750, 600);
background(0);
}
void draw(){
colPicker = new colourPicker(0, 0, 150, 100); //draw colour picker at origin
colPicker.drawTable(); //draw colour picker
colPicker.colourSelect(); //draw colour selected and get selected colour
m = new Mandala(32); //create mandala with 32 segments
m.createCanvas(150, 0, width-150, height); //draw mandala canvas
m.draw(colPicker.c); //set colour of pen to the colour picker selected colour
}
void keyPressed(){
if(key == 's'){
saveFrame("####.png"); //save when key 's' is pressed
}
}