Colorx

From Unify Community Wiki
Jump to: navigation, search

Author: Jonathan Czeck (aarku)

Contents

Description

This script provides a helper function to tie in with HSBColor that will let you slerp between two Unity Colors. The slerp function provides much more pleasing visual results than Color.Lerp.

Usage

Put this script in a file in /Plugins/Colorx.cs so that it may be callable by JavaScript. Additionally, there is a helper class called HSBColor that this relies on. This script, Colorx.cs, provides a quick helper function to slerp between two Colors, which gives much more visually pleasing results than Color.Lerp.

Unity Package

Download the attached package and import it into one of your projects.

HSBColor.unityPackage.zip

C# - Colorx.cs

using UnityEngine;
 
public class Colorx
{
    public static Color Slerp(Color a, Color b, float t)
    {
        return (HSBColor.Lerp(HSBColor.FromColor(a), HSBColor.FromColor(b), t)).ToColor();
    }
}


C# - Extended Colorx.cs

This version of Colorx adds functions to manipulate H, S, and B values of any Color. By: Josh Koleszar (talk) 23:35, 20 October 2013 (CEST)


using UnityEngine;
using System.Collections;
 
public static class Colorx 
{
    // These functions are accessible from any Color struct.  
    // (Put this script in the Plugins folder for Javascript access)
 
    public static Color Slerp(this Color a, Color b, float t)
    {
        return (HSBColor.Lerp(HSBColor.FromColor(a), HSBColor.FromColor(b), t)).ToColor();
    }
 
    // To use the following functions, you must input the Color you wish to change.
    // Example:
    // myColor.H(180, ref myColor);
 
    // You can either manipulate the values on a scale from 0 to 360 or
    // on a scale from 0 to 1.
 
    public static void H(this Color c, int hue0to360, ref Color thisColor)
    {
        HSBColor temp = HSBColor.FromColor(c);
        temp.h = (hue0to360 / 360.0f);
        thisColor = HSBColor.ToColor(temp);
    }
 
    public static void H(this Color c, float hue0to1, ref Color thisColor)
    {
        HSBColor temp = HSBColor.FromColor(thisColor);
        temp.h = hue0to1;
        thisColor = HSBColor.ToColor(temp);
    }
 
    public static void S(this Color c, int saturation0to360, ref Color thisColor)
    {
        HSBColor temp = HSBColor.FromColor(thisColor);
        temp.s = saturation0to360 / 360.0f;
        thisColor = HSBColor.ToColor(temp);
    }
 
    public static void S(this Color c, float saturation0to1, ref Color thisColor)
    {
        HSBColor temp = HSBColor.FromColor(thisColor);
        temp.s = saturation0to1;
        thisColor = HSBColor.ToColor(temp);
    }
 
    public static void B(this Color c, int brightness0to360, ref Color thisColor)
    {
        HSBColor temp = HSBColor.FromColor(thisColor);
        temp.b = brightness0to360 / 360.0f;
        thisColor = HSBColor.ToColor(temp);
    }
 
    public static void B(this Color c, float brightness0to1, ref Color thisColor)
    {
        HSBColor temp = HSBColor.FromColor(thisColor);
        temp.b = brightness0to1;
        thisColor = HSBColor.ToColor(temp);
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox