Created
May 26, 2020 15:15
-
-
Save KrabCode/7ade6b64fceb9f06db7a2d2e277a6424 to your computer and use it in GitHub Desktop.
Color gradient function with adjustable colors and color position.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void setup() { | |
| size(800,800, P2D); | |
| } | |
| void draw() { | |
| float[] positions = new float[]{0, 0.25, 0.5, 0.55, 1}; | |
| int[] colors = new int[]{ | |
| color(0), | |
| color(4,38,102), | |
| color(255,255,255), | |
| color(68,144,201), | |
| color(0) | |
| }; | |
| ramp(g, true, positions, colors); | |
| } | |
| protected void ramp(PGraphics pg, boolean vertical, float[] positions, int[] colors) { | |
| if(positions.length != colors.length){ | |
| throw new IllegalArgumentException(); | |
| } | |
| pg.hint(PConstants.DISABLE_DEPTH_TEST); | |
| float prevY = 0; | |
| float prevR = 0; | |
| for (int i = 1; i < positions.length; i++) { | |
| float pos = positions[i]; | |
| int previousColor = colors[i-1]; | |
| int currentColor = colors[i]; | |
| pg.noStroke(); | |
| pg.beginShape(TRIANGLE_STRIP); | |
| if (vertical) { | |
| int verticalDetail = 10; | |
| float y = pos * pg.height; | |
| for (int j = 0; j < verticalDetail; j++) { | |
| float x = map(j, 0, verticalDetail - 1, 0, width); | |
| pg.fill(previousColor); | |
| pg.vertex(x, prevY); | |
| pg.fill(currentColor); | |
| pg.vertex(x, y); | |
| } | |
| prevY = y; | |
| } else { | |
| int circularDetail = 100; | |
| float diagonalLength = dist(0, 0, pg.width / 2f, pg.height / 2f); | |
| float r = pos * diagonalLength; | |
| for (int j = 0; j < circularDetail; j++) { | |
| float theta = map(j, 0, circularDetail - 1, 0, TAU); | |
| pg.fill(previousColor); | |
| pg.vertex(pg.width / 2f + prevR * cos(theta), pg.height / 2f + prevR * sin(theta)); | |
| pg.fill(currentColor); | |
| pg.vertex(pg.width / 2f + r * cos(theta), pg.height / 2f + r * sin(theta)); | |
| } | |
| prevR = r; | |
| } | |
| pg.endShape(); | |
| } | |
| pg.hint(PConstants.ENABLE_DEPTH_TEST); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment