Java Code Examples for com.badlogic.gdx.math.MathUtils#round()

The following examples show how to use com.badlogic.gdx.math.MathUtils#round() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedColorPicker.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
/** Updates picker from R, G and B bars */
private void updateValuesFromRGBFields () {
	int r = MathUtils.round(color.r * 255.0f);
	int g = MathUtils.round(color.g * 255.0f);
	int b = MathUtils.round(color.b * 255.0f);

	if (rBar.isInputValid()) r = rBar.getValue();
	if (gBar.isInputValid()) g = gBar.getValue();
	if (bBar.isInputValid()) b = bBar.getValue();

	color.set(r / 255.0f, g / 255.0f, b / 255.0f, color.a);

	int[] hsv = ColorUtils.RGBtoHSV(color);
	int ch = hsv[0];
	int cs = hsv[1];
	int cv = hsv[2];

	hBar.setValue(ch);
	sBar.setValue(cs);
	vBar.setValue(cv);

	verticalBar.setValue(hBar.getValue());
	palette.setValue(sBar.getValue(), vBar.getValue());
}
 
Example 2
Source File: ExtendedColorPicker.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
/** Updates picker from H, S and V bars */
@Override
protected void updateValuesFromHSVFields () {
	int[] hsv = ColorUtils.RGBtoHSV(color);
	int h = hsv[0];
	int s = hsv[1];
	int v = hsv[2];

	if (hBar.isInputValid()) h = hBar.getValue();
	if (sBar.isInputValid()) s = sBar.getValue();
	if (vBar.isInputValid()) v = vBar.getValue();

	color = ColorUtils.HSVtoRGB(h, s, v, color.a);

	int cr = MathUtils.round(color.r * 255.0f);
	int cg = MathUtils.round(color.g * 255.0f);
	int cb = MathUtils.round(color.b * 255.0f);

	rBar.setValue(cr);
	gBar.setValue(cg);
	bBar.setValue(cb);
}
 
Example 3
Source File: ExtendedColorPicker.java    From vis-ui with Apache License 2.0 6 votes vote down vote up
@Override
protected void updateValuesFromCurrentColor () {
	int[] hsv = ColorUtils.RGBtoHSV(color);
	int ch = hsv[0];
	int cs = hsv[1];
	int cv = hsv[2];

	int cr = MathUtils.round(color.r * 255.0f);
	int cg = MathUtils.round(color.g * 255.0f);
	int cb = MathUtils.round(color.b * 255.0f);
	int ca = MathUtils.round(color.a * 255.0f);

	hBar.setValue(ch);
	sBar.setValue(cs);
	vBar.setValue(cv);

	rBar.setValue(cr);
	gBar.setValue(cg);
	bBar.setValue(cb);

	aBar.setValue(ca);

	verticalBar.setValue(hBar.getValue());
	palette.setValue(sBar.getValue(), vBar.getValue());
}
 
Example 4
Source File: BaseScorer.java    From Klooni1010 with GNU General Public License v3.0 6 votes vote down vote up
public void draw(SpriteBatch batch) {
    // If we beat a new record, the cup color will linear interpolate to the high score color
    cupColor.lerp(isNewRecord() ? Klooni.theme.highScore : Klooni.theme.currentScore, 0.05f);
    batch.setColor(cupColor);
    batch.draw(cupTexture, cupArea.x, cupArea.y, cupArea.width, cupArea.height);

    int roundShown = MathUtils.round(shownScore);
    if (roundShown != currentScore) {
        shownScore = Interpolation.linear.apply(shownScore, currentScore, 0.1f);
        currentScoreLabel.setText(Integer.toString(MathUtils.round(shownScore)));
    }

    currentScoreLabel.setColor(Klooni.theme.currentScore);
    currentScoreLabel.draw(batch, 1f);

    highScoreLabel.setColor(Klooni.theme.highScore);
    highScoreLabel.draw(batch, 1f);
}
 
Example 5
Source File: ColorUtils.java    From typing-label with MIT License 5 votes vote down vote up
/**
 * Converts RGB to HSV color system
 *
 * @param r red 0-1
 * @param g green 0-1
 * @param b blue 0-1
 * @return 3 element int array with hue (0-360), saturation (0-100) and value (0-100)
 */
public static int[] RGBtoHSV(float r, float g, float b) {
    float h, s, v;
    float min, max, delta;

    min = Math.min(Math.min(r, g), b);
    max = Math.max(Math.max(r, g), b);
    v = max;

    delta = max - min;

    if(max != 0)
        s = delta / max;
    else {
        s = 0;
        h = 0;
        return new int[]{MathUtils.round(h), MathUtils.round(s), MathUtils.round(v)};
    }

    if(delta == 0)
        h = 0;
    else {

        if(r == max)
            h = (g - b) / delta;
        else if(g == max)
            h = 2 + (b - r) / delta;
        else
            h = 4 + (r - g) / delta;
    }

    h *= 60;
    if(h < 0)
        h += 360;

    s *= 100;
    v *= 100;

    return new int[]{MathUtils.round(h), MathUtils.round(s), MathUtils.round(v)};
}
 
Example 6
Source File: ColorUtils.java    From vis-ui with Apache License 2.0 5 votes vote down vote up
/**
 * Converts RGB to HSV color system
 * @param r red 0-1
 * @param g green 0-1
 * @param b blue 0-1
 * @return 3 element int array with hue (0-360), saturation (0-100) and value (0-100)
 */
public static int[] RGBtoHSV (float r, float g, float b) {
	float h, s, v;
	float min, max, delta;

	min = Math.min(Math.min(r, g), b);
	max = Math.max(Math.max(r, g), b);
	v = max;

	delta = max - min;

	if (max != 0)
		s = delta / max;
	else {
		s = 0;
		h = 0;
		return new int[]{MathUtils.round(h), MathUtils.round(s), MathUtils.round(v)};
	}

	if (delta == 0)
		h = 0;
	else {

		if (r == max)
			h = (g - b) / delta;
		else if (g == max)
			h = 2 + (b - r) / delta;
		else
			h = 4 + (r - g) / delta;
	}

	h *= 60;
	if (h < 0)
		h += 360;

	s *= 100;
	v *= 100;

	return new int[]{MathUtils.round(h), MathUtils.round(s), MathUtils.round(v)};
}
 
Example 7
Source File: IsometricCamera.java    From riiablo with Apache License 2.0 5 votes vote down vote up
/**
 * Rounds tile coords to the closest integer tile coords from the specified float tile coords.
 */
public Vector2 toTile50(float x, float y, Vector2 dst) {
  x += tileOffset.x;
  y += tileOffset.y;
  dst.x = x < 0 ? MathUtils.round(x) : MathUtils.roundPositive(x);
  dst.y = y < 0 ? MathUtils.round(y) : MathUtils.roundPositive(y);
  return dst;
}
 
Example 8
Source File: RepeatTextureDrawable.java    From gdx-vfx with Apache License 2.0 5 votes vote down vote up
/** Shift in normalized values [0..1] */
public void setShift(float shiftFactorX, float shiftFactorY) {
    shiftFactorX %= 1.0f;
    shiftFactorY %= 1.0f;
    this.shiftX = MathUtils.round(texture.getWidth() * shiftFactorX);
    this.shiftY = MathUtils.round(texture.getHeight() * shiftFactorY);
}
 
Example 9
Source File: Board.java    From Klooni1010 with GNU General Public License v3.0 5 votes vote down vote up
Vector2 snapToGrid(final Piece piece, final Vector2 position) {
    // Snaps the given position (e.g. mouse) to the grid,
    // assuming piece wants to be put at the specified position.
    // If the piece was not on the grid, the original position is returned
    //
    // Logic to determine the x and y is a copy-paste from putScreenPiece
    final Vector2 local = position.cpy().sub(pos);
    int x = MathUtils.round(local.x / piece.cellSize);
    int y = MathUtils.round(local.y / piece.cellSize);
    if (canPutPiece(piece, x, y))
        return new Vector2(pos.x + x * piece.cellSize, pos.y + y * piece.cellSize);
    else
        return position;
}
 
Example 10
Source File: Board.java    From Klooni1010 with GNU General Public License v3.0 5 votes vote down vote up
public boolean putScreenPiece(final Piece piece) {
    // Convert the on screen coordinates of the piece to the local-board-space coordinates
    // This is done by subtracting the piece coordinates from the board coordinates
    Vector2 local = piece.pos.cpy().sub(pos);
    int x = MathUtils.round(local.x / piece.cellSize);
    int y = MathUtils.round(local.y / piece.cellSize);
    return putPiece(piece, x, y);
}
 
Example 11
Source File: CaveGenerator.java    From xibalba with MIT License 5 votes vote down vote up
private MapCell.Type[][] blank() {
  MapCell.Type[][] newGeo = geometry.clone();

  int rows = 2;
  int start = MathUtils.round(geometry[0].length / 2) - rows;

  for (int x = 0; x < geometry.length; x++) {
    for (int y = start; y < start + (rows - 1); y++) {
      newGeo[x][y] = MapCell.Type.WALL;
    }
  }

  return newGeo;
}
 
Example 12
Source File: ForestGenerator.java    From xibalba with MIT License 5 votes vote down vote up
private MapCell.Type[][] blank() {
  MapCell.Type[][] newGeo = geometry.clone();

  int rows = 2;
  int start = MathUtils.round(geometry[0].length / 2) - rows;

  for (int x = 0; x < geometry.length; x++) {
    for (int y = start; y < start + (rows - 1); y++) {
      newGeo[x][y] = MapCell.Type.FLOOR;
    }
  }

  return newGeo;
}
 
Example 13
Source File: ColorUtils.java    From typing-label with MIT License 4 votes vote down vote up
/**
 * Converts HSV color system to RGB
 *
 * @param h           hue 0-360
 * @param s           saturation 0-100
 * @param v           value 0-100
 * @param targetColor color that result will be stored in
 * @return targetColor
 */
public static Color HSVtoRGB(float h, float s, float v, Color targetColor) {
    if(h == 360) h = 359;
    int r, g, b;
    int i;
    float f, p, q, t;
    h = (float) Math.max(0.0, Math.min(360.0, h));
    s = (float) Math.max(0.0, Math.min(100.0, s));
    v = (float) Math.max(0.0, Math.min(100.0, v));
    s /= 100;
    v /= 100;
    h /= 60;
    i = MathUtils.floor(h);
    f = h - i;
    p = v * (1 - s);
    q = v * (1 - s * f);
    t = v * (1 - s * (1 - f));
    switch(i) {
        case 0:
            r = MathUtils.round(255 * v);
            g = MathUtils.round(255 * t);
            b = MathUtils.round(255 * p);
            break;
        case 1:
            r = MathUtils.round(255 * q);
            g = MathUtils.round(255 * v);
            b = MathUtils.round(255 * p);
            break;
        case 2:
            r = MathUtils.round(255 * p);
            g = MathUtils.round(255 * v);
            b = MathUtils.round(255 * t);
            break;
        case 3:
            r = MathUtils.round(255 * p);
            g = MathUtils.round(255 * q);
            b = MathUtils.round(255 * v);
            break;
        case 4:
            r = MathUtils.round(255 * t);
            g = MathUtils.round(255 * p);
            b = MathUtils.round(255 * v);
            break;
        default:
            r = MathUtils.round(255 * v);
            g = MathUtils.round(255 * p);
            b = MathUtils.round(255 * q);
    }

    targetColor.set(r / 255.0f, g / 255.0f, b / 255.0f, targetColor.a);
    return targetColor;
}
 
Example 14
Source File: MathHelper.java    From dice-heroes with GNU General Public License v3.0 4 votes vote down vote up
public static float snapToNearest(float value, float snap) {
    if (snap == 0)
        return value;
    return MathUtils.round(value / snap) * snap;
}
 
Example 15
Source File: Map.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public static int round(float i) {
  return MathUtils.round(i);
}
 
Example 16
Source File: ColorUtils.java    From vis-ui with Apache License 2.0 4 votes vote down vote up
/**
 * Converts HSV color system to RGB
 * @param h hue 0-360
 * @param s saturation 0-100
 * @param v value 0-100
 * @param targetColor color that result will be stored in
 * @return targetColor
 */
public static Color HSVtoRGB (float h, float s, float v, Color targetColor) {
	if (h == 360) h = 359;
	int r, g, b;
	int i;
	float f, p, q, t;
	h = (float) Math.max(0.0, Math.min(360.0, h));
	s = (float) Math.max(0.0, Math.min(100.0, s));
	v = (float) Math.max(0.0, Math.min(100.0, v));
	s /= 100;
	v /= 100;
	h /= 60;
	i = MathUtils.floor(h);
	f = h - i;
	p = v * (1 - s);
	q = v * (1 - s * f);
	t = v * (1 - s * (1 - f));
	switch (i) {
		case 0:
			r = MathUtils.round(255 * v);
			g = MathUtils.round(255 * t);
			b = MathUtils.round(255 * p);
			break;
		case 1:
			r = MathUtils.round(255 * q);
			g = MathUtils.round(255 * v);
			b = MathUtils.round(255 * p);
			break;
		case 2:
			r = MathUtils.round(255 * p);
			g = MathUtils.round(255 * v);
			b = MathUtils.round(255 * t);
			break;
		case 3:
			r = MathUtils.round(255 * p);
			g = MathUtils.round(255 * q);
			b = MathUtils.round(255 * v);
			break;
		case 4:
			r = MathUtils.round(255 * t);
			g = MathUtils.round(255 * p);
			b = MathUtils.round(255 * v);
			break;
		default:
			r = MathUtils.round(255 * v);
			g = MathUtils.round(255 * p);
			b = MathUtils.round(255 * q);
	}

	targetColor.set(r / 255.0f, g / 255.0f, b / 255.0f, targetColor.a);
	return targetColor;
}
 
Example 17
Source File: NumberUtils.java    From talos with Apache License 2.0 4 votes vote down vote up
public static float roundToDecimalPlaces (float number, int amountOfPlaces) {
    float roundTo = (float) Math.pow(10, amountOfPlaces);
    return MathUtils.round(number * roundTo) / roundTo;
}