add Color abstract

This commit is contained in:
2019-03-11 17:02:34 +03:00
parent 8c0991adb0
commit 62ddbb683e
16 changed files with 196 additions and 153 deletions

View File

@@ -0,0 +1,53 @@
package haxework.color;
abstract Color(Int) {
public var alpha(get, never):Int;
public var red(get, never):Int;
public var green(get, never):Int;
public var blue(get, never):Int;
public var zero(get, never):Bool;
public inline function new(value:Int) {
this = value;
}
private inline function get_alpha():Int {
return (this >> 24) & 255;
}
private inline function get_red():Int {
return (this >> 16) & 255;
}
private inline function get_green():Int {
return (this >> 8) & 255;
}
private inline function get_blue():Int {
return this & 255;
}
private inline function get_zero():Bool {
return green == 0 && red == 0 && blue == 0;
}
@:from static public inline function fromArray(value:Array<Int>):Color {
return new Color((value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3]);
}
@:from static public inline function fromInt(value:Int):Color {
return new Color(value);
}
@:to public inline function toInt():Int {
return this;
}
@:from static public inline function fromString(value:String):Color {
return new Color(Std.parseInt('0x${value.split('#').pop()}'));
}
@:to public inline function toString():String {
return StringTools.hex(this);
}
}

View File

@@ -0,0 +1,26 @@
package haxework.color;
class ColorUtil {
public static function floor(colorPart:Float):Int {
return Std.int(Math.max(0, Math.min(255, colorPart)));
}
public static function multiply(color:Color, m:Float):Color {
return [
color.alpha,
floor(color.red * m),
floor(color.green * m),
floor(color.blue * m),
];
}
public static function diff(color:Color, d:Int):Color {
return [
color.alpha,
floor(color.red + d),
floor(color.green + d),
floor(color.blue + d),
];
}
}

View File

@@ -1,9 +1,9 @@
package haxework.gui;
import haxework.gui.core.Geometry.Position;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import haxework.gui.core.Geometry.Position;
import haxework.gui.list.ScrollBarView;
import haxework.gui.skin.Skin;
import haxework.signal.Signal;
@@ -28,7 +28,7 @@ class ScrollView extends HGroupView {
private function onMouseWheelEvent(event:MouseEvent):Void {
#if flash event.preventDefault(); #end
position -= event.delta / 50;
position -= event.delta * scroll.ratio / 3;
}
private function set_view(value:IView<Dynamic>):IView<Dynamic> {

View File

@@ -1,54 +0,0 @@
package haxework.gui.list;
import haxework.gui.skin.ISkin;
class HScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
view.content.graphics.beginFill(backColor);
view.content.graphics.drawRect(0, 0, view.width, view.height);
view.content.graphics.beginFill(foreColor);
view.content.graphics.drawRect(view.width * view.position, 0, view.width * view.ratio, view.height);
view.content.graphics.endFill();
}
}
}
class VScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
view.content.graphics.beginFill(backColor);
view.content.graphics.drawRect(0, 0, view.width, view.height);
view.content.graphics.beginFill(foreColor);
view.content.graphics.drawRect(0, view.height * view.position, view.width, view.height * view.ratio);
view.content.graphics.endFill();
}
}
}
class ScrollBarSkin {
public static function horizontal(foreColor:Int = 0xffffff, backColor:Int = 0x707070) {
return new HScrollBarSkin(foreColor, backColor);
}
public static function vertical(foreColor:Int = 0xffffff, backColor:Int = 0x707070) {
return new VScrollBarSkin(foreColor, backColor);
}
}

View File

@@ -1,7 +1,7 @@
package haxework.gui.skin;
import flash.display.Graphics;
import haxework.gui.utils.ColorUtils;
import haxework.color.ColorUtil;
import haxework.gui.ButtonView.ButtonState;
class ButtonColorSkin implements ISkin<ButtonView> {
@@ -19,9 +19,9 @@ class ButtonColorSkin implements ISkin<ButtonView> {
private function set_color(value:Int):Int {
colors = new Map<ButtonState, Int>();
colors.set(ButtonState.UP, value);
colors.set(ButtonState.DOWN, ColorUtils.diff(value, -32));
colors.set(ButtonState.OVER, ColorUtils.diff(value, 32));
//disable = ColorUtils.multiply(value, 0.6);
colors.set(ButtonState.DOWN, ColorUtil.diff(value, -32));
colors.set(ButtonState.OVER, ColorUtil.diff(value, 32));
//disable = ColorUtil.multiply(value, 0.6);
return value;
}
@@ -29,12 +29,12 @@ class ButtonColorSkin implements ISkin<ButtonView> {
var color:Int = selectColor(view);
if (Std.is(view, ToggleButtonView)) {
if (!cast(view, ToggleButtonView).on) {
color = ColorUtils.multiply(color, 0.5);
color = ColorUtil.multiply(color, 0.5);
}
}
var graphics:Graphics = view.content.graphics;
graphics.beginFill(color, alpha);
graphics.lineStyle(2, ColorUtils.multiply(color, 1.5));
graphics.lineStyle(2, ColorUtil.multiply(color, 1.5));
graphics.drawRoundRect(1, 1, view.width - 2, view.height - 2, 5, 5);
}

View File

@@ -0,0 +1,29 @@
package haxework.gui.skin;
import haxework.color.ColorUtil;
import haxework.gui.list.ScrollBarView;
class HScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
var graphics = view.content.graphics;
var size = Math.max(view.width * view.ratio, 15);
var position = Math.min(view.width * view.position, view.width - size);
graphics.beginFill(backColor);
graphics.lineStyle(2, ColorUtil.multiply(foreColor, 1.5));
graphics.drawRect(0, 0, view.width, view.height);
graphics.beginFill(foreColor);
graphics.lineStyle();
graphics.drawRect(position, 0, size, view.height);
graphics.endFill();
}
}
}

View File

@@ -4,19 +4,19 @@ import flash.display.BitmapData;
class Skin {
public static function size(width:Float, height:Float): ISkin<Dynamic> {
public static function size(width:Float, height:Float):ISkin<Dynamic> {
return new SizeSkin(width, height);
}
public static function bitmap(image:BitmapData): ISkin<SpriteView> {
public static function bitmap(image:BitmapData):ISkin<SpriteView> {
return new BitmapSkin(image);
}
public static function color(color: Int, alpha: Float = 1.0): ISkin<SpriteView> {
public static function color(color:Int, alpha:Float = 1.0):ISkin<SpriteView> {
return new ColorSkin(color, alpha);
}
public static function border(color: Int, alpha: Float = 1.0, tickness: Float = 1.0): ISkin<SpriteView> {
public static function border(color:Int, alpha:Float = 1.0, tickness:Float = 1.0):ISkin<SpriteView> {
return new BorderSkin(color, alpha, tickness);
}
@@ -24,11 +24,23 @@ class Skin {
return new TextSkin(fontColor, fontSize, fontFamily);
}
public static function buttonColor(color: Int, alpha: Float = 1.0): ISkin<ButtonView> {
public static function buttonColor(color:Int, alpha:Float = 1.0):ISkin<ButtonView> {
return new ButtonColorSkin(color, alpha);
}
public static function tabColor(color: Int, alpha: Float = 1.0): ISkin<ButtonView> {
public static function buttonBitmap(image:BitmapData):ISkin<ButtonView> {
return new ButtonBitmapSkin(image);
}
public static function tabColor(color:Int, alpha:Float = 1.0):ISkin<ButtonView> {
return new TabColorSkin(color, alpha);
}
public static function scrollHorizontal(foreColor:Int, backColor:Int) {
return new HScrollBarSkin(foreColor, backColor);
}
public static function scrollVertical(foreColor:Int, backColor:Int) {
return new VScrollBarSkin(foreColor, backColor);
}
}

View File

@@ -1,7 +1,7 @@
package haxework.gui.skin;
import flash.display.Graphics;
import haxework.gui.utils.ColorUtils;
import haxework.color.ColorUtil;
class TabColorSkin extends ButtonColorSkin {
@@ -9,12 +9,12 @@ class TabColorSkin extends ButtonColorSkin {
var color:Int = selectColor(view);
if (Std.is(view, ToggleButtonView)) {
if (!cast(view, ToggleButtonView).on) {
color = ColorUtils.multiply(color, 0.5);
color = ColorUtil.multiply(color, 0.5);
}
}
var graphics:Graphics = view.content.graphics;
graphics.beginFill(color, alpha);
graphics.lineStyle(2, ColorUtils.multiply(color, 1.5));
graphics.lineStyle(2, ColorUtil.multiply(color, 1.5));
graphics.drawRoundRectComplex(1, 1, view.width - 2, view.height - 2, 5, 5, 0, 0);
}
}

View File

@@ -0,0 +1,29 @@
package haxework.gui.skin;
import haxework.color.ColorUtil;
import haxework.gui.list.ScrollBarView;
class VScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
var graphics = view.content.graphics;
var size = Math.max(view.height * view.ratio, 15);
var position = Math.min(view.height * view.position, view.height - size);
graphics.beginFill(backColor);
graphics.lineStyle(2, ColorUtil.multiply(foreColor, 1.5));
graphics.drawRect(0, 0, view.width, view.height);
graphics.beginFill(foreColor);
graphics.lineStyle();
graphics.drawRect(0, position, view.width, size);
graphics.endFill();
}
}
}

View File

@@ -1,31 +0,0 @@
package haxework.gui.utils;
class ColorUtils {
public static function multiply(color:Int, m:Float):Int {
var rgb:Array<Int> = color2rgb(color);
var red:Int = cast Math.min(255, Math.round(rgb[0] * m));
var green:Int = cast Math.min(255, Math.round(rgb[1] * m));
var blue:Int = cast Math.min(255, Math.round(rgb[2] * m));
return rgb2color(red, green, blue);
}
public static function diff(color:Int, d:Int):Int {
var rgb:Array<Int> = color2rgb(color);
var red:Int = cast Math.max(0, Math.min(255, rgb[0] + d));
var green:Int = cast Math.max(0, Math.min(255, rgb[1] + d));
var blue:Int = cast Math.max(0, Math.min(255, rgb[2] + d));
return rgb2color(red, green, blue);
}
public static function rgb2color(red:Int, green:Int, blue:Int):Int {
return (red << 16) + (green << 8) + blue;
}
public static function color2rgb(color:Int):Array<Int> {
var red:Int = ((color & 0xFF0000) >>> 16);
var green:Int = ((color & 0x00FF00) >> 8);
var blue:Int = (color & 0x0000FF);
return [red, green, blue];
}
}