michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import org.mozilla.gecko.util.FloatUtils; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: public class FloatSize { michael@0: public final float width, height; michael@0: michael@0: public FloatSize(FloatSize size) { width = size.width; height = size.height; } michael@0: public FloatSize(IntSize size) { width = size.width; height = size.height; } michael@0: public FloatSize(float aWidth, float aHeight) { width = aWidth; height = aHeight; } michael@0: michael@0: public FloatSize(JSONObject json) { michael@0: try { michael@0: width = (float)json.getDouble("width"); michael@0: height = (float)json.getDouble("height"); michael@0: } catch (JSONException e) { michael@0: throw new RuntimeException(e); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { return "(" + width + "," + height + ")"; } michael@0: michael@0: public boolean isPositive() { michael@0: return (width > 0 && height > 0); michael@0: } michael@0: michael@0: public boolean fuzzyEquals(FloatSize size) { michael@0: return (FloatUtils.fuzzyEquals(size.width, width) && michael@0: FloatUtils.fuzzyEquals(size.height, height)); michael@0: } michael@0: michael@0: public FloatSize scale(float factor) { michael@0: return new FloatSize(width * factor, height * factor); michael@0: } michael@0: michael@0: /* michael@0: * Returns the size that represents a linear transition between this size and `to` at time `t`, michael@0: * which is on the scale [0, 1). michael@0: */ michael@0: public FloatSize interpolate(FloatSize to, float t) { michael@0: return new FloatSize(FloatUtils.interpolate(width, to.width, t), michael@0: FloatUtils.interpolate(height, to.height, t)); michael@0: } michael@0: } michael@0: