Class Utils

java.lang.Object
com.dlsc.gemsfx.util.Utils

public class Utils extends Object
Some basic utilities which need to be in java (for shifting operations or other reasons), which are not toolkit dependent.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    calculateBrightness(javafx.scene.paint.Color color)
    Calculates a perceptual brightness for a color between 0.0 and 1.0.
    static double
    clamp(double min, double value, double max)
    Clamps the given value to the inclusive range between min and max.
    static float
    clamp(float min, float value, float max)
    Clamps the given value to the inclusive range between min and max.
    static int
    clamp(int min, int value, int max)
    Clamps the given value to the inclusive range between min and max.
    static long
    clamp(long min, long value, long max)
    Clamps the given value to the inclusive range between min and max.
    static int
    clampMax(int value, int max)
    Clamps the given value to be at most max.
    static double
    clampMin(double value, double min)
    Clamps the given value to be at least min.
    static boolean
    Checks whether the source string contains the given substring.
    static javafx.scene.paint.Color
    convertLinearRGBtoSRGB(javafx.scene.paint.Color color)
    Converts a color from linear RGB space to sRGB space.
    static javafx.scene.paint.Color
    convertSRGBtoLinearRGB(javafx.scene.paint.Color color)
    Converts a color from sRGB space to linear RGB space.
    static javafx.scene.paint.Color
    deriveColor(javafx.scene.paint.Color c, double brightness)
    Derives a lighter or darker version of a given color.
    static javafx.stage.Screen
    Determines the best screen for the given object.
    static javafx.stage.Screen
    getScreenForPoint(double x, double y)
    Determines the best screen for the given point.
    static javafx.stage.Screen
    getScreenForRectangle(javafx.geometry.Rectangle2D rect)
    Determines the best screen for the given rectangle.
    static boolean
    hasFullScreenStage(javafx.stage.Screen screen)
    Checks whether the given screen currently shows a full-screen stage.
    static double[]
    HSBtoRGB(double hue, double saturation, double brightness)
    Converts HSB values to RGB values.
    static boolean
    Returns whether the primary screen has QVGA dimensions.
    static javafx.scene.paint.Color
    ladder(javafx.scene.paint.Color color, javafx.scene.paint.Stop[] stops)
    Returns the ladder color for the given base color and stops.
    static double
    nearest(double less, double value, double more)
    Returns either less or more, depending on which one is closer to value.
    static javafx.geometry.Point2D
    pointRelativeTo(Object parent, double width, double height, double screenX, double screenY, javafx.geometry.HPos hpos, javafx.geometry.VPos vpos)
    Repositions an item relative to its parent while keeping it on screen whenever possible.
    static javafx.geometry.Point2D
    pointRelativeTo(javafx.scene.Node parent, double anchorWidth, double anchorHeight, javafx.geometry.HPos hpos, javafx.geometry.VPos vpos, double dx, double dy, boolean reposition)
    Computes a point for positioning content relative to its parent.
    static javafx.geometry.Point2D
    pointRelativeTo(javafx.scene.Node parent, javafx.scene.Node node, javafx.geometry.HPos hpos, javafx.geometry.VPos vpos, double dx, double dy, boolean reposition)
    Computes a point for positioning a node relative to its parent.
    static double[]
    RGBtoHSB(double r, double g, double b)
    Converts RGB values to HSB values.
    static String[]
    split(String str, String separator)
    Splits the given string using the specified separator.
    static String
    Removes leading and trailing quotes from a string.
    static double
    sum(double[] values)
    Calculates the average of the given values.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • clamp

      public static float clamp(float min, float value, float max)
      Clamps the given value to the inclusive range between min and max.
      Parameters:
      min - the minimum allowed value
      value - the value to clamp
      max - the maximum allowed value
      Returns:
      the clamped value
    • clamp

      public static int clamp(int min, int value, int max)
      Clamps the given value to the inclusive range between min and max.
      Parameters:
      min - the minimum allowed value
      value - the value to clamp
      max - the maximum allowed value
      Returns:
      the clamped value
    • clamp

      public static double clamp(double min, double value, double max)
      Clamps the given value to the inclusive range between min and max.
      Parameters:
      min - the minimum allowed value
      value - the value to clamp
      max - the maximum allowed value
      Returns:
      the clamped value
    • clamp

      public static long clamp(long min, long value, long max)
      Clamps the given value to the inclusive range between min and max.
      Parameters:
      min - the minimum allowed value
      value - the value to clamp
      max - the maximum allowed value
      Returns:
      the clamped value
    • clampMin

      public static double clampMin(double value, double min)
      Clamps the given value to be at least min.
      Parameters:
      value - the value to clamp
      min - the minimum allowed value
      Returns:
      the clamped value
    • clampMax

      public static int clampMax(int value, int max)
      Clamps the given value to be at most max.
      Parameters:
      value - the value to clamp
      max - the maximum allowed value
      Returns:
      the clamped value
    • nearest

      public static double nearest(double less, double value, double more)
      Returns either less or more, depending on which one is closer to value.
      Parameters:
      less - the lower candidate value
      value - the value to compare against
      more - the upper candidate value
      Returns:
      the nearest candidate value
    • stripQuotes

      public static String stripQuotes(String str)
      Removes leading and trailing quotes from a string. Works with single or double quotes.
      Parameters:
      str - the string to process
      Returns:
      the unquoted string
    • split

      public static String[] split(String str, String separator)
      Splits the given string using the specified separator.
      Parameters:
      str - the string to split
      separator - the separator to use
      Returns:
      the split parts
    • contains

      public static boolean contains(String src, String s)
      Checks whether the source string contains the given substring.
      Parameters:
      src - the source string
      s - the substring to look for
      Returns:
      true if the substring is contained in the source string
    • calculateBrightness

      public static double calculateBrightness(javafx.scene.paint.Color color)
      Calculates a perceptual brightness for a color between 0.0 and 1.0.
      Parameters:
      color - the color to analyze
      Returns:
      the calculated brightness
    • deriveColor

      public static javafx.scene.paint.Color deriveColor(javafx.scene.paint.Color c, double brightness)
      Derives a lighter or darker version of a given color.
      Parameters:
      c - the color to derive from
      brightness - the brightness difference, where -1.0 is black, 0.0 means no change, and 1.0 is white
      Returns:
      the derived color
    • ladder

      public static javafx.scene.paint.Color ladder(javafx.scene.paint.Color color, javafx.scene.paint.Stop[] stops)
      Returns the ladder color for the given base color and stops.
      Parameters:
      color - the base color
      stops - the ladder stops
      Returns:
      the ladder color
    • HSBtoRGB

      public static double[] HSBtoRGB(double hue, double saturation, double brightness)
      Converts HSB values to RGB values.
      Parameters:
      hue - the hue value
      saturation - the saturation value
      brightness - the brightness value
      Returns:
      the RGB values
    • RGBtoHSB

      public static double[] RGBtoHSB(double r, double g, double b)
      Converts RGB values to HSB values.
      Parameters:
      r - the red component
      g - the green component
      b - the blue component
      Returns:
      the HSB values
    • convertSRGBtoLinearRGB

      public static javafx.scene.paint.Color convertSRGBtoLinearRGB(javafx.scene.paint.Color color)
      Converts a color from sRGB space to linear RGB space.
      Parameters:
      color - the color to convert
      Returns:
      the converted color
    • convertLinearRGBtoSRGB

      public static javafx.scene.paint.Color convertLinearRGBtoSRGB(javafx.scene.paint.Color color)
      Converts a color from linear RGB space to sRGB space.
      Parameters:
      color - the color to convert
      Returns:
      the converted color
    • sum

      public static double sum(double[] values)
      Calculates the average of the given values.
      Parameters:
      values - the values to average
      Returns:
      the calculated average
    • pointRelativeTo

      public static javafx.geometry.Point2D pointRelativeTo(javafx.scene.Node parent, javafx.scene.Node node, javafx.geometry.HPos hpos, javafx.geometry.VPos vpos, double dx, double dy, boolean reposition)
      Computes a point for positioning a node relative to its parent.
      Parameters:
      parent - the parent node
      node - the node to position
      hpos - the horizontal position
      vpos - the vertical position
      dx - the horizontal offset
      dy - the vertical offset
      reposition - whether the point should be adjusted to remain on screen
      Returns:
      the computed point
    • pointRelativeTo

      public static javafx.geometry.Point2D pointRelativeTo(javafx.scene.Node parent, double anchorWidth, double anchorHeight, javafx.geometry.HPos hpos, javafx.geometry.VPos vpos, double dx, double dy, boolean reposition)
      Computes a point for positioning content relative to its parent.
      Parameters:
      parent - the parent node
      anchorWidth - the width of the content to position
      anchorHeight - the height of the content to position
      hpos - the horizontal position
      vpos - the vertical position
      dx - the horizontal offset
      dy - the vertical offset
      reposition - whether the point should be adjusted to remain on screen
      Returns:
      the computed point
    • pointRelativeTo

      public static javafx.geometry.Point2D pointRelativeTo(Object parent, double width, double height, double screenX, double screenY, javafx.geometry.HPos hpos, javafx.geometry.VPos vpos)
      Repositions an item relative to its parent while keeping it on screen whenever possible.
      Parameters:
      parent - the parent object
      width - the width of the item to position
      height - the height of the item to position
      screenX - the initial x coordinate
      screenY - the initial y coordinate
      hpos - the horizontal position
      vpos - the vertical position
      Returns:
      the computed point
    • hasFullScreenStage

      public static boolean hasFullScreenStage(javafx.stage.Screen screen)
      Checks whether the given screen currently shows a full-screen stage.
      Parameters:
      screen - the screen to inspect
      Returns:
      true if a full-screen stage is shown on the screen
    • isQVGAScreen

      public static boolean isQVGAScreen()
      Returns whether the primary screen has QVGA dimensions.
      Returns:
      true if the primary screen uses QVGA dimensions
    • getScreen

      public static javafx.stage.Screen getScreen(Object obj)
      Determines the best screen for the given object.
      Parameters:
      obj - the object to inspect
      Returns:
      the best matching screen
    • getScreenForRectangle

      public static javafx.stage.Screen getScreenForRectangle(javafx.geometry.Rectangle2D rect)
      Determines the best screen for the given rectangle.
      Parameters:
      rect - the rectangle to inspect
      Returns:
      the best matching screen
    • getScreenForPoint

      public static javafx.stage.Screen getScreenForPoint(double x, double y)
      Determines the best screen for the given point.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      the best matching screen