Skip to content

Instantly share code, notes, and snippets.

@marvin-roesch
Created July 30, 2015 21:32
Show Gist options
  • Save marvin-roesch/e4e9b1b34431056ff041 to your computer and use it in GitHub Desktop.
Save marvin-roesch/e4e9b1b34431056ff041 to your computer and use it in GitHub Desktop.
Color cycling
object Color {
def black(alpha: Float) = this(0, 0, 0, alpha)
def fromHSV(hue: Float, saturation: Float, value: Float): Color = fromHSV(hue, saturation, value, 1)
def fromHSV(hue: Float, saturation: Float, value: Float, alpha: Float): Color = apply(java.awt.Color.HSBtoRGB(hue, saturation, value), alpha)
def apply(hex: Int): Color = {
val r = ((hex >> 16) & 0xFF) / 255F
val g = ((hex >> 8) & 0xFF) / 255F
val b = (hex & 0xFF) / 255F
val a = ((hex >> 24) & 0xFF) / 255F
this(r, g, b, if (a > 0) a else 1)
}
def apply(hex: Int, alpha: Float): Color = {
val r = ((hex >> 16) & 0xFF) / 255F
val g = ((hex >> 8) & 0xFF) / 255F
val b = (hex & 0xFF) / 255F
this(r, g, b, alpha)
}
def apply(r: Float, g: Float, b: Float): Color = this(r, g, b, 1)
}
case class Color(r: Float, g: Float, b: Float, a: Float) {
def integer = {
var i = ((a * 255).toInt & 0xFF) << 24
i |= ((r * 255).toInt & 0xFF) << 16
i |= ((g * 255).toInt & 0xFF) << 8
i |= (b * 255).toInt & 0xFF
i
}
def rInt = (r * 255).toInt
def gInt = (g * 255).toInt
def bInt = (b * 255).toInt
def aInt = (a * 255).toInt
def mix(color: Color) = Color((color.r + r) / 2f, (color.g + g) / 2f, (color.b + b) / 2f, (color.a + a) / 2f)
def lighten(amount: Float) = {
val hsv = java.awt.Color.RGBtoHSB((r * 255).toInt, (g * 255).toInt, (b * 255).toInt, null)
Color.fromHSV(hsv(0), hsv(1), MathHelper.clamp_float(hsv(2) + amount, 0, 1), a)
}
def darken(amount: Float) = {
lighten(-amount)
}
}
class ColorCyclingItem extends Item(CreativeTabs.BLOCKS) {
@SideOnly(Side.CLIENT)
override def getColorFromItemStack(stack: ItemStack, pass: Int): Int =
Color.fromHSV(MathUtils.scale(Minecraft.getSystemTime % 36000, 0, 36000, 0, 1).toFloat, 1, 1).integer
}
object MathUtils {
def easeInOut(time: Double, start: Double, change: Double, duration: Double): Double = {
var t = time / (duration / 2)
if (t < 1) return change / 2 * t * t + start
t -= 1
-change / 2 * (t * (t - 2) - 1) + start
}
def pulsate(time: Double, start: Double, change: Double, duration: Double): Double = {
abs(start + change * sin(2 * Pi * time * (1 / (duration * 2))))
}
def scale(valueIn: Double, baseMin: Double, baseMax: Double, limitMin: Double, limitMax: Double): Double =
((limitMax - limitMin) * (valueIn - baseMin) / (baseMax - baseMin)) + limitMin
def wrapAround(value: Int, max: Int, min: Int = 0) = if (value > max) min else value
def intToByteArray(value: Int): Array[Byte] =
Array((value >>> 24).toByte, (value >>> 16).toByte, (value >>> 8).toByte, value.toByte)
def between(min: Double, value: Double, max: Double) = min <= value && value <= max
def round(value: Double, precision: Int) = BigDecimal(value).setScale(precision, BigDecimal.RoundingMode.HALF_UP).toDouble
def createProjectionMatrixAsPerspective(fovDegrees: Double, near: Double, far: Double, viewportWidth: Int, viewportHeight: Int) = {
// for impl details see gluPerspective doco in OpenGL reference manual
val aspect = viewportWidth.toDouble / viewportHeight.toDouble
val theta = Math.toRadians(fovDegrees) / 2d
val f = Math.cos(theta) / Math.sin(theta)
val a = (far + near) / (near - far)
val b = (2d * far * near) / (near - far)
new Matrix4(f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, a, b, 0, 0, -1, 0)
}
def createMatrixAsLookAt(eye: Vector3, lookAt: Vector3, up: Vector3) = {
val forwardVec = (lookAt - eye).normalize
val sideVec = forwardVec.cross(up).normalize
val upVed = sideVec.cross(forwardVec).normalize
val mat = new Matrix4(
sideVec.x, sideVec.y, sideVec.z, 0,
upVed.x, upVed.y, upVed.z, 0,
-forwardVec.x, -forwardVec.y, -forwardVec.z, 0,
0, 0, 0, 1)
mat.withTranslation(mat.transformNormal(-eye))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment