Skip to content

Instantly share code, notes, and snippets.

@AbelToy
Last active August 29, 2015 14:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AbelToy/6e28392bc0bec79876d5 to your computer and use it in GitHub Desktop.
Save AbelToy/6e28392bc0bec79876d5 to your computer and use it in GitHub Desktop.
Luxe Gradient Component
package;
import luxe.Color;
import luxe.Component;
import luxe.Sprite;
/**
* This will set the color of any Sprite to a gradient.
*
* @author Abel Toy
* @license The MIT License (https://gist.github.com/Rolpege/6e28392bc0bec79876d5#file-license)
*/
enum GradientDirection
{
Vertical;
Horizontal;
}
class Gradient extends Component
{
public var colorA(get, set):Color;
public var colorB(get, set):Color;
public var direction(get, set):GradientDirection;
var _sprite:Sprite;
var _colorA:Color;
var _colorB:Color;
var _direction:GradientDirection = Vertical;
public function new(colorA:Color, colorB:Color, ?direction:GradientDirection)
{
super({ name: "gradient" });
_colorA = colorA;
_colorB = colorB;
if (direction == null) direction = Vertical;
_direction = direction;
}
override public function init()
{
_sprite = cast entity;
_updateColors();
}
function _updateColors():Void
{
if (_sprite == null) return;
if(_direction == Vertical)
{
_sprite.geometry.vertices[0].color = _colorA;
_sprite.geometry.vertices[1].color = _colorA;
_sprite.geometry.vertices[4].color = _colorA;
_sprite.geometry.vertices[2].color = _colorB;
_sprite.geometry.vertices[3].color = _colorB;
_sprite.geometry.vertices[5].color = _colorB;
}
else
{
_sprite.geometry.vertices[0].color = _colorA;
_sprite.geometry.vertices[3].color = _colorA;
_sprite.geometry.vertices[4].color = _colorA;
_sprite.geometry.vertices[1].color = _colorB;
_sprite.geometry.vertices[2].color = _colorB;
_sprite.geometry.vertices[5].color = _colorB;
}
}
function get_colorA():Color
{
return _colorA;
}
function set_colorA(value:Color):Color
{
_colorA = value;
_updateColors();
return value;
}
function get_colorB():Color
{
return _colorB;
}
function set_colorB(value:Color):Color
{
_colorB = value;
_updateColors();
return value;
}
function get_direction():GradientDirection
{
return _direction;
}
function set_direction(value:GradientDirection):GradientDirection
{
if (value == _direction) return value;
_direction = value;
_updateColors();
return value;
}
}
The MIT License (MIT)
Copyright (c) 2014, Abel Toy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@anissen
Copy link

anissen commented Dec 29, 2014

_sprite should be of type Visual to make it more general (as Visual is a supertype of Sprite)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment