Skip to content

Commit

Permalink
(#10, #3) Added unit tests for random gradients, add constants for mi…
Browse files Browse the repository at this point in the history
…nimum color count/radius.

- Moved random gradient methods to `Gradients.java`
  • Loading branch information
lucasstarsz committed Jun 6, 2021
1 parent 281e036 commit c082670
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 130 deletions.
48 changes: 0 additions & 48 deletions src/main/java/tech/fastj/graphics/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Maths;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.util.gradients.Gradients;
import tech.fastj.graphics.util.gradients.LinearGradientBuilder;
import tech.fastj.graphics.util.gradients.RadialGradientBuilder;

import java.awt.Color;
import java.awt.Font;
Expand Down Expand Up @@ -584,48 +580,4 @@ public static Font randomFont() {

return new Font(fontNames[Maths.randomInteger(0, fontNames.length - 1)].getFontName(), font, Maths.randomInteger(1, 256));
}

public static LinearGradientPaint randomLinearGradient(Drawable drawable, Boundary start, Boundary end) {
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(drawable, start, end);

int randomColorCount = Maths.randomInteger(2, 8);
for (int i = 0; i < randomColorCount; i++) {
linearGradientBuilder.withColor(randomColor());
}

return linearGradientBuilder.build();
}

public static LinearGradientPaint randomLinearGradientWithAlpha(Drawable drawable, Boundary start, Boundary end) {
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(drawable, start, end);

int randomColorCount = Maths.randomInteger(2, 8);
for (int i = 0; i < randomColorCount; i++) {
linearGradientBuilder.withColor(randomColorWithAlpha());
}

return linearGradientBuilder.build();
}

public static RadialGradientPaint randomRadialGradient(Drawable drawable) {
RadialGradientBuilder radialGradientBuilder = Gradients.radialGradient(drawable);

int randomColorCount = Maths.randomInteger(2, 8);
for (int i = 0; i < randomColorCount; i++) {
radialGradientBuilder.withColor(randomColor());
}

return radialGradientBuilder.build();
}

public static RadialGradientPaint randomRadialGradientWithAlpha(Drawable drawable) {
RadialGradientBuilder radialGradientBuilder = Gradients.radialGradient(drawable);

int randomColorCount = Maths.randomInteger(2, 8);
for (int i = 0; i < randomColorCount; i++) {
radialGradientBuilder.withColor(randomColorWithAlpha());
}

return radialGradientBuilder.build();
}
}
174 changes: 166 additions & 8 deletions src/main/java/tech/fastj/graphics/util/gradients/Gradients.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tech.fastj.graphics.util.gradients;

import tech.fastj.math.Maths;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.util.DrawUtil;

import java.awt.Color;
import java.awt.GradientPaint;
Expand All @@ -14,8 +16,14 @@
/** Class to streamline working with the {@link GradientPaint} class (and its implementations) within FastJ. */
public class Gradients {

/** The amount of colors allowed in a single {@link LinearGradientBuilder} or {@link RadialGradientBuilder}. */
public static final int ColorLimit = 8;
/** The maximum amount of colors allowed in a single {@link LinearGradientBuilder} or {@link RadialGradientBuilder}. */
public static final int MaximumColorCount = 8;

/** The minimum amount of colors allowed in a single {@link LinearGradientBuilder} or {@link RadialGradientBuilder}. */
public static final int MinimumColorCount = 2;

/** The minimum radius allowed in a single {@link RadialGradientBuilder}, including the value of this constant. */
public static final float MinimumRadius = 0.0f;

/**
* Gets a builder instance for creating a {@link LinearGradientPaint}.
Expand Down Expand Up @@ -53,6 +61,156 @@ public static RadialGradientBuilder radialGradient(Pointf center, float radius)
return RadialGradientBuilder.builder(center, radius);
}

/**
* Generates a random linear gradient from the specified {@code drawable} and {@code start}/{@code end} values, with
* random colors.
*
* @param drawable The {@link Drawable} to calculate start/end {@code Pointf}s.
* @param start The {@link Boundary} specifying the starting area for the gradient.
* @param end The {@link Boundary} specifying the ending area for the gradient.
* @return The randomly generated {@link LinearGradientPaint}.
*/
public static LinearGradientPaint randomLinearGradient(Drawable drawable, Boundary start, Boundary end) {
LinearGradientBuilder linearGradientBuilder = linearGradient(drawable, start, end);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
linearGradientBuilder.withColor(DrawUtil.randomColor());
}

return linearGradientBuilder.build();
}

/**
* Generates a random linear gradient from the specified {@code drawable} and {@code start}/{@code end} values, with
* random colors including random alpha values.
*
* @param drawable The {@link Drawable} to calculate start/end {@code Pointf}s.
* @param start The {@link Boundary} specifying the starting area for the gradient.
* @param end The {@link Boundary} specifying the ending area for the gradient.
* @return The randomly generated {@link LinearGradientPaint}.
*/
public static LinearGradientPaint randomLinearGradientWithAlpha(Drawable drawable, Boundary start, Boundary end) {
LinearGradientBuilder linearGradientBuilder = linearGradient(drawable, start, end);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
linearGradientBuilder.withColor(DrawUtil.randomColorWithAlpha());
}

return linearGradientBuilder.build();
}

/**
* Generates a random linear gradient from the specified {@code start}/{@code end} values, with random colors.
*
* @param start The {@link Pointf} specifying the starting area for the gradient.
* @param end The {@link Pointf} specifying the ending area for the gradient.
* @return The randomly generated {@link LinearGradientPaint}.
*/
public static LinearGradientPaint randomLinearGradient(Pointf start, Pointf end) {
LinearGradientBuilder linearGradientBuilder = linearGradient(start, end);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
linearGradientBuilder.withColor(DrawUtil.randomColor());
}

return linearGradientBuilder.build();
}

/**
* Generates a random linear gradient from the specified {@code start}/{@code end} values, with random colors
* including random alpha values.
*
* @param start The {@link Pointf} specifying the starting area for the gradient.
* @param end The {@link Pointf} specifying the ending area for the gradient.
* @return The randomly generated {@link LinearGradientPaint}.
*/
public static LinearGradientPaint randomLinearGradientWithAlpha(Pointf start, Pointf end) {
LinearGradientBuilder linearGradientBuilder = linearGradient(start, end);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
linearGradientBuilder.withColor(DrawUtil.randomColorWithAlpha());
}

return linearGradientBuilder.build();
}

/**
* Generates a random radial gradient from the specified {@code drawable}, with random colors.
*
* @param drawable The {@link Drawable} specifying the center and radius for the gradient.
* @return The randomly generated {@link RadialGradientPaint}.
*/
public static RadialGradientPaint randomRadialGradient(Drawable drawable) {
RadialGradientBuilder radialGradientBuilder = radialGradient(drawable);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
radialGradientBuilder.withColor(DrawUtil.randomColor());
}

return radialGradientBuilder.build();
}

/**
* Generates a random radial gradient from the specified {@code drawable}, with random colors including random alpha
* values.
*
* @param drawable The {@link Drawable} specifying the center and radius for the gradient.
* @return The randomly generated {@link RadialGradientPaint}.
*/
public static RadialGradientPaint randomRadialGradientWithAlpha(Drawable drawable) {
RadialGradientBuilder radialGradientBuilder = radialGradient(drawable);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
radialGradientBuilder.withColor(DrawUtil.randomColorWithAlpha());
}

return radialGradientBuilder.build();
}

/**
* Generates a random radial gradient from the specified {@code center} and {@code radius} values, with random
* colors.
*
* @param center The {@link Pointf} specifying the centerpoint for the gradient.
* @param radius The {@code float} specifying the radius for the gradient.
* @return The randomly generated {@link RadialGradientPaint}.
*/
public static RadialGradientPaint randomRadialGradient(Pointf center, float radius) {
RadialGradientBuilder radialGradientBuilder = radialGradient(center, radius);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
radialGradientBuilder.withColor(DrawUtil.randomColor());
}

return radialGradientBuilder.build();
}

/**
* Generates a random radial gradient from the specified {@code center} and {@code radius} values, with random
* colors including random alpha values.
*
* @param center The {@link Pointf} specifying the centerpoint for the gradient.
* @param radius The {@code float} specifying the radius for the gradient.
* @return The randomly generated {@link RadialGradientPaint}.
*/
public static RadialGradientPaint randomRadialGradientWithAlpha(Pointf center, float radius) {
RadialGradientBuilder radialGradientBuilder = radialGradient(center, radius);

int randomColorCount = Maths.randomInteger(MinimumColorCount, MaximumColorCount);
for (int i = 0; i < randomColorCount; i++) {
radialGradientBuilder.withColor(DrawUtil.randomColorWithAlpha());
}

return radialGradientBuilder.build();
}

static void samePointfCheck(Pointf start, Pointf end) {
if (start.equals(end)) {
throw new IllegalArgumentException(
Expand All @@ -74,7 +232,7 @@ static void sameBoundaryCheck(Boundary start, Boundary end) {
}

static void minimumRadiusValueCheck(float radius) {
if (radius <= 0f) {
if (radius <= MinimumRadius) {
throw new IllegalArgumentException("The radius for a gradient must be larger than 0.");
}
}
Expand All @@ -92,19 +250,19 @@ static void nullColorCheck(Color[] colors) {
}

static void colorLimitCheck(int count) {
if (count >= Gradients.ColorLimit) {
throw new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.");
if (count >= MaximumColorCount) {
throw new IllegalStateException("Gradients cannot contain more than " + MaximumColorCount + " colors.");
}
}

static void colorLimitCheck(int count, Color[] colors) {
if (count + colors.length > Gradients.ColorLimit) {
throw new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.");
if (count + colors.length > MaximumColorCount) {
throw new IllegalStateException("Gradients cannot contain more than " + MaximumColorCount + " colors.");
}
}

static void minimumColorCheck(int count, Color[] colors) {
if (count < 2) {
if (count < MinimumColorCount) {
throw new IllegalStateException(
"Gradients must contain at least 2 colors."
+ System.lineSeparator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class LinearGradientBuilder implements GradientBuilder {

/** Initializes a {@code LinearGradientBuilder}'s internals. */
LinearGradientBuilder(Drawable drawable, Boundary start, Boundary end) {
colors = new Color[Gradients.ColorLimit];
colors = new Color[Gradients.MaximumColorCount];
position(drawable, start, end);
}

/** Initializes a {@code LinearGradientBuilder}'s internals. */
LinearGradientBuilder(Pointf start, Pointf end) {
colors = new Color[Gradients.ColorLimit];
colors = new Color[Gradients.MaximumColorCount];
position(start, end);
}

Expand Down Expand Up @@ -60,7 +60,7 @@ private void position(Pointf start, Pointf end) {
/**
* Adds a {@link Color} to the builder to be used in the resulting gradient.
* <p>
* The amount of colors used in a {@link LinearGradientBuilder} cannot exceed {@link Gradients#ColorLimit}.
* The amount of colors used in a {@link LinearGradientBuilder} cannot exceed {@link Gradients#MaximumColorCount}.
*
* @param color The {@code Color} being added.
* @return The {@code LinearGradientBuilder}, for method chaining.
Expand All @@ -77,7 +77,7 @@ public LinearGradientBuilder withColor(Color color) {
/**
* Adds several {@link Color}s to the builder to be used in the resulting gradient.
* <p>
* The amount of colors used in a {@link LinearGradientBuilder} cannot exceed {@link Gradients#ColorLimit}.
* The amount of colors used in a {@link LinearGradientBuilder} cannot exceed {@link Gradients#MaximumColorCount}.
*
* @param colors The {@code Color}s being added. This parameter must not cause the builder to increase over 8
* colors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class RadialGradientBuilder implements GradientBuilder {

/** Initializes a {@code RadialGradientBuilder}'s internals. */
RadialGradientBuilder(Drawable drawable) {
colors = new Color[Gradients.ColorLimit];
colors = new Color[Gradients.MaximumColorCount];
position(drawable);
}

/** Initializes a {@code RadialGradientBuilder}'s internals. */
RadialGradientBuilder(Pointf center, float radius) {
colors = new Color[Gradients.ColorLimit];
colors = new Color[Gradients.MaximumColorCount];
position(center, radius);
}

Expand Down Expand Up @@ -57,7 +57,7 @@ private void position(Pointf center, float radius) {
/**
* Adds a {@link Color} to the builder to be used in the resulting gradient.
* <p>
* The amount of colors used in a {@link RadialGradientBuilder} not exceed {@link Gradients#ColorLimit}.
* The amount of colors used in a {@link RadialGradientBuilder} not exceed {@link Gradients#MaximumColorCount}.
*
* @param color The {@code Color} being added.
* @return The {@code LinearGradientBuilder}, for method chaining.
Expand All @@ -74,7 +74,7 @@ public RadialGradientBuilder withColor(Color color) {
/**
* Adds several {@link Color}s to the builder to be used in the resulting gradient.
* <p>
* The amount of colors used in a {@link RadialGradientBuilder} cannot exceed {@link Gradients#ColorLimit}.
* The amount of colors used in a {@link RadialGradientBuilder} cannot exceed {@link Gradients#MaximumColorCount}.
*
* @param colors The {@code Color}s being added. This parameter must not cause the builder to increase over 8
* colors.
Expand Down
Loading

0 comments on commit c082670

Please sign in to comment.