Skip to content

Commit

Permalink
Remove PrefsIntKey and PrefsURIListKey.
Browse files Browse the repository at this point in the history
Remove MathUtil.clamp methods, because java.lang.Math provides these methods now.
  • Loading branch information
wrandelshofer committed Aug 9, 2024
1 parent ae73dbf commit fcf3c07
Show file tree
Hide file tree
Showing 31 changed files with 145 additions and 344 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.jhotdraw8.color;

import javafx.scene.paint.Color;
import org.jhotdraw8.color.util.MathUtil;

import java.awt.color.ColorSpace;

Expand Down Expand Up @@ -90,7 +89,7 @@ public Color interpolate(double t) {
interpolated[hueComponent] = interp;
}
float[] srgb = cs.toRGB(interpolated);
return new Color(MathUtil.clamp(srgb[0], 0, 1), MathUtil.clamp(srgb[1], 0, 1), MathUtil.clamp(srgb[2], 0, 1), 1);
return new Color(Math.clamp(srgb[0], (float) 0, (float) 1), Math.clamp(srgb[1], (float) 0, (float) 1), Math.clamp(srgb[2], (float) 0, (float) 1), 1);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.jhotdraw8.color;

import javafx.scene.paint.Color;
import org.jhotdraw8.color.util.MathUtil;


public class FXColorUtil {
Expand Down Expand Up @@ -37,7 +36,7 @@ public static float[] fromColor(NamedColorSpace colorSpace, Color c) {
*/
public static Color toColor(NamedColorSpace colorSpace, float[] components) {
float[] srgb = colorSpace.toRGB(components);
return new Color(MathUtil.clamp(srgb[0], 0, 1), MathUtil.clamp(srgb[1], 0, 1), MathUtil.clamp(srgb[2], 0, 1), 1);
return new Color(Math.clamp(srgb[0], (float) 0, (float) 1), Math.clamp(srgb[1], (float) 0, (float) 1), Math.clamp(srgb[2], (float) 0, (float) 1), 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.awt.color.ColorSpace;
import java.io.Serial;

import static org.jhotdraw8.color.util.MathUtil.clamp;

/**
* A parametric HLS color space computed from an RGB color space.
* <p>
Expand Down Expand Up @@ -157,9 +155,9 @@ protected float[] hlsToRgb(float[] hls, float[] rgb) {
blue = p;
}

rgb[0] = (float) clamp(red, 0d, 1d);
rgb[1] = (float) clamp(green, 0d, 1d);
rgb[2] = (float) clamp(blue, 0d, 1d);
rgb[0] = (float) Math.clamp(red, 0d, 1d);
rgb[1] = (float) Math.clamp(green, 0d, 1d);
rgb[2] = (float) Math.clamp(blue, 0d, 1d);
return rgb;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package org.jhotdraw8.color;

import static org.jhotdraw8.color.util.MathUtil.clamp;

/**
* Provides conversion functions between RGB bit representations.
*/
Expand Down Expand Up @@ -182,7 +180,7 @@ public static int rgbFloatToArgb32(float[] rgb, float alpha) {
|| rgb[0] > EPSILON + 1 || rgb[1] > EPSILON + 1 || rgb[2] > EPSILON + 1) {
return 0;
}*/
return (clamp((int) (alpha * 255), 0, 255) << 24) | rgbFloatToRgb24(rgb);
return (Math.clamp((int) (alpha * 255), 0, 255) << 24) | rgbFloatToRgb24(rgb);
}

public static int rgbFloatToPreArgb32(float[] rgb, float alpha, float[] pre) {
Expand All @@ -192,11 +190,11 @@ public static int rgbFloatToPreArgb32(float[] rgb, float alpha, float[] pre) {
|| rgb[0] > EPSILON + 1 || rgb[1] > EPSILON + 1 || rgb[2] > EPSILON + 1) {
return 0;
}*/
alpha = clamp(alpha, 0, 1);
alpha = Math.clamp(alpha, (float) 0, (float) 1);
pre[0] = rgb[0] * alpha;
pre[1] = rgb[1] * alpha;
pre[2] = rgb[2] * alpha;
return (clamp((int) (alpha * 255), 0, 255) << 24) | rgbFloatToRgb24(pre);
return (Math.clamp((int) (alpha * 255), 0, 255) << 24) | rgbFloatToRgb24(pre);
}

/**
Expand All @@ -206,9 +204,9 @@ public static int rgbFloatToPreArgb32(float[] rgb, float alpha, float[] pre) {
* @return 24-bit RGB color value
*/
public static int rgbFloatToRgb24(float[] rgb) {
return (clamp((int) ((rgb[0] + 1f / 512) * 255f), 0, 255) << 16)
| (clamp((int) ((rgb[1] + 1f / 512) * 255f), 0, 255) << 8)
| clamp((int) ((rgb[2] + 1f / 512) * 255f), 0, 255);
return (Math.clamp((int) ((rgb[0] + 1f / 512) * 255f), 0, 255) << 16)
| (Math.clamp((int) ((rgb[1] + 1f / 512) * 255f), 0, 255) << 8)
| Math.clamp((int) ((rgb[2] + 1f / 512) * 255f), 0, 255);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,6 @@ public class MathUtil {
private MathUtil() {
}

/**
* Clamps a value to the given range.
*
* @param value the value
* @param min the lower bound of the range
* @param max the upper bound of the range
* @return the constrained value
*/
public static float clamp(float value, float min, float max) {
if (Float.isNaN(value) || value < min) {
return min;
} else if (value > max) {
return max;
}
return value;
}

/**
* Clamps a value to the given range.
*
* @param value the value
* @param min the lower bound of the range
* @param max the upper bound of the range
* @return the constrained value
*/
public static double clamp(double value, double min, double max) {
if (Double.isNaN(value) || value < min) {
return min;
} else if (value > max) {
return max;
}
return value;
}

/**
* Clamps a value to the given range.
*
* @param value the value
* @param min the lower bound of the range
* @param max the upper bound of the range
* @return the constrained value
*/
public static int clamp(int value, int min, int max) {
if (value < min) {
return min;
}
return Math.min(value, max);
}

public static boolean almostEqual(float a, float b, float eps) {
return Math.abs(a - b) < eps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.List;
import java.util.function.Consumer;

import static org.jhotdraw8.base.util.MathUtil.clamp;

/**
* CssColorConverter.
* <p>
Expand Down Expand Up @@ -205,12 +203,13 @@ yield switch (tt.currentStringNonNull().toLowerCase()) {
throw tt.createParseException("Could not convert a string to a CssColor because the closing bracket ')' is missing.");
}
float[] rgb = clampColors(params);
double value = params.get(3).getValue();
return new CssColor(
"color("
+ colorSpaceParam + " "
+ colorParamToString(params)
+ ")",
new Color(rgb[0], rgb[1], rgb[2], params.size() == 4 ? clamp(params.get(3).getValue(), 0, 1) : 1.0));
new Color(rgb[0], rgb[1], rgb[2], params.size() == 4 ? Math.clamp(value, 0, 1) : 1.0));
}

private static List<CssSize> parseParams(CssTokenizer tt, NamedColorSpace cs) throws IOException, ParseException {
Expand Down Expand Up @@ -280,7 +279,13 @@ private CssColor parseRgbFunction(CssTokenizer tt) throws ParseException, IOExce
rgb[i] = rgb[i] / 255f;
}
var clamped = clampColors(rgb);
float alpha = params.size() == 4 ? clamp(toPercentage(params.get(3), 0.01, tt), 0, 1) : 1;
float alpha;
if (params.size() == 4) {
float value = toPercentage(params.get(3), 0.01, tt);
alpha = Math.clamp(value, (float) 0, (float) 1);
} else {
alpha = 1;
}
return new CssColor(
"rgb(" + colorParamToString(params) + ")",
new Color(clamped[0], clamped[1], clamped[2], alpha));
Expand All @@ -294,7 +299,13 @@ private CssColor parseHslFunction(CssTokenizer tt) throws ParseException, IOExce
toPercentage(params.get(1), 1 / 100d, tt)
};
float[] rgb = clampColors(CSS_HLS_COLOR_SPACE.toRGB(hls));
float alpha = params.size() == 4 ? clamp(toPercentage(params.get(3), 0.01, tt), 0, 1) : 1;
float alpha;
if (params.size() == 4) {
float value = toPercentage(params.get(3), 0.01, tt);
alpha = Math.clamp(value, (float) 0, (float) 1);
} else {
alpha = 1;
}
return new CssColor(
"hsl(" + colorParamToString(params) + ")",
new Color(rgb[0], rgb[1], rgb[2], alpha)
Expand All @@ -310,7 +321,13 @@ private CssColor parseHsbFunction(CssTokenizer tt) throws ParseException, IOExce
};
float[] rgb = JAVAFX_HSB_COLOR_SPACE.toRGB(hsb);
float[] clamped = clampColors(rgb);
float alpha = params.size() == 4 ? clamp(toPercentage(params.get(3), 0.01, tt), 0, 1) : 1;
float alpha;
if (params.size() == 4) {
float value = toPercentage(params.get(3), 0.01, tt);
alpha = Math.clamp(value, (float) 0, (float) 1);
} else {
alpha = 1;
}
return new CssColor(
"hsb(" + colorParamToString(params) + ")",
new Color(clamped[0], clamped[1], clamped[2], alpha));
Expand Down Expand Up @@ -360,7 +377,7 @@ private static float[] clampColors(List<CssSize> params) {
private static float[] clampColors(float[] params) {
float[] clamped = new float[3];
for (int i = 0; i < clamped.length; i++) {
clamped[i] = clamp(params[i], 0, 1);
clamped[i] = Math.clamp(params[i], (float) 0, (float) 1);
}
return clamped;
}
Expand Down
Loading

0 comments on commit fcf3c07

Please sign in to comment.