Skip to content

Commit 60499dd

Browse files
Merge pull request #9 from vivek-30/gradient-brush
feat: Add Rainbow Brush
2 parents e66f1d2 + 2f5fa14 commit 60499dd

File tree

6 files changed

+105
-5
lines changed

6 files changed

+105
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ See [example](https://harshkhandeparkar.github.io/gpujs-real-renderer).
229229
- `brushColor` (*array*): The color of the brush in the corm `[red, green, blue]` where each of
230230
`red`, `green` and `blue` are between 0 and 1.
231231
- `eraserSize` (*number*): Size of the eraser.
232-
- `tool` ('brush' | 'eraser' | 'line'): The current tool used on the board. This tool can be set in the options or using the `changeTool` method.
232+
- `tool` ('brush' | 'rainbow_brush' | 'eraser' | 'line'): The current tool used on the board. This tool can be set in the options or using the `changeTool` method.
233233
234234
##### Options
235235
Since this is a child class of `RealRenderer`, all the options of `RealRender` are applicable here as well.
@@ -245,7 +245,7 @@ Apart from those, the following are additional options that can be passed on to
245245
246246
- `maxUndos`(*Number*) (Default: `15`): Determines the maximum possible undos. (Use a smaller number on devices with less RAM)
247247
248-
- `tool` (*'brush' | 'draw' | 'line'*) (Default: `'brush'`): Determines which tool to use.
248+
- `tool` (*'brush' | 'rainbow_brush' | 'eraser' | 'line'*) (Default: `'brush'`): Determines which tool to use.
249249
250250
##### Methods
251251
Since this is a child class of `RealRenderer`, all the methods of `RealRender` are available here as well.

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ <h3># Drawing Board</h3>
4848
<label for="draw-tool">Tool: </label>
4949
<select name="draw-tool" id="draw-tool" value="brush">
5050
<option value="brush">Brush</option>
51+
<option value="rainbow_brush">Rainbow Brush</option>
5152
<option value="eraser">Eraser</option>
5253
<option value="line">Line</option>
5354
</select>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { RealDrawBoard } from '../RealDrawBoard';
2+
import { Texture } from 'gpu.js';
3+
import { convertHSLToRGB } from '../../../util/convertHSLToRGB';
4+
5+
let hue: number = 0;
6+
let gradientColors: [number, number, number] = [1, 1, 1];
7+
8+
export const name = 'rainbow_brush';
9+
10+
export function _startStroke(
11+
this: RealDrawBoard,
12+
coords: [number, number],
13+
identifier: string
14+
) {
15+
gradientColors = convertHSLToRGB(hue, 90, 40);
16+
this._doPreview = false;
17+
this._plot(coords[0], coords[1], this.brushSize, gradientColors);
18+
}
19+
20+
export function _endStroke(
21+
this: RealDrawBoard,
22+
endCoords: [number, number],
23+
identifier: string
24+
) {
25+
gradientColors = convertHSLToRGB(hue, 90, 40);
26+
this._plot(endCoords[0], endCoords[1], this.brushSize, gradientColors);
27+
this._doPreview = true;
28+
}
29+
30+
export function _doStroke(
31+
this: RealDrawBoard,
32+
coords: [number, number],
33+
identifier: string
34+
) {
35+
hue = (hue + 1) % 360;
36+
gradientColors = convertHSLToRGB(hue, 90, 40);
37+
this._plot(coords[0], coords[1], this.brushSize, gradientColors);
38+
this._stroke(coords[0], coords[1], this.brushSize, gradientColors, identifier);
39+
}
40+
41+
export function _toolPreview(
42+
this: RealDrawBoard,
43+
coords: [number, number],
44+
identifier: string
45+
): Texture {
46+
return <Texture>this._previewPlot(
47+
this.graphPixels,
48+
coords[0],
49+
coords[1],
50+
this.brushSize,
51+
gradientColors
52+
)
53+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as brush from './brush';
22
import * as eraser from './eraser';
33
import * as line from './line';
4+
import * as rainbow_brush from './rainbow_brush';
45

56
export const tools = {
67
brush,
8+
rainbow_brush,
79
eraser,
810
line
911
}
1012

11-
export type Tool = 'brush' | 'eraser' | 'line';
13+
export type Tool = 'brush' | 'rainbow_brush' | 'eraser' | 'line';

src/util/convertForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export function convertPolarCartesian(r: number, theta: number) {
1313
/**
1414
* Convert Cartesian to polar form.
1515
* @param x Real Part
16-
* @param theta Complex Part
16+
* @param y Complex Part
1717
*/
1818
export function convertCartesianPolar(x: number, y: number) {
1919
return [
2020
Math.sqrt(x*x + y*y),
2121
Math.atan2(y, x)
2222
]
23-
}
23+
}

src/util/convertHSLToRGB.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Convert hsl Color into RGB color.
3+
* @param h color value ranges from 0 to 360.
4+
* @param s saturation value.
5+
* @param l brightness level.
6+
* @return array containing r g and b value
7+
*/
8+
export function convertHSLToRGB(h: number, s: number, l: number): [number, number, number] {
9+
10+
s /= 100;
11+
l /= 100;
12+
13+
let c: number = (1 - Math.abs(2 * l - 1)) * s;
14+
let x: number = c * (1 - Math.abs((h / 60) % 2 - 1));
15+
let m: number = l - c/2;
16+
let r: number = 0;
17+
let g: number = 0;
18+
let b: number = 0;
19+
20+
if (0 <= h && h < 60) {
21+
r = c; g = x; b = 0;
22+
}
23+
else if (60 <= h && h < 120) {
24+
r = x; g = c; b = 0;
25+
}
26+
else if (120 <= h && h < 180) {
27+
r = 0; g = c; b = x;
28+
}
29+
else if (180 <= h && h < 240) {
30+
r = 0; g = x; b = c;
31+
}
32+
else if (240 <= h && h < 300) {
33+
r = x; g = 0; b = c;
34+
}
35+
else if (300 <= h && h < 360) {
36+
r = c; g = 0; b = x;
37+
}
38+
39+
r = (r + m);
40+
g = (g + m);
41+
b = (b + m);
42+
43+
return [r, g, b];
44+
}

0 commit comments

Comments
 (0)