[]颜色滤镜 差值变化
/**
* leo
*/
module touch {
/**
* 颜色工具类
*/
export class ColorTool {
private _targetsValue: number = 16777215;
private _targetsLastValue: number = null;
private targets: Array<Laya.Sprite> = null;
public static instance: ColorTool = null;
private targetsFilter: Laya.ColorFilter = null;
private _persent: number = 0;
protected constructor() {
this.targets = new Array<Laya.Sprite>();
}
/**
* 实例
*/
public static getInstance(): ColorTool {
if (ColorTool.instance == null) {
ColorTool.instance = new ColorTool();
}
return ColorTool.instance;
}
/**
* 添加颜色改变对象
* @param target
*/
public addColorTarget(target: Laya.Sprite): void {
this.targets.push(target);
}
/**
* 设置颜色改变值
* @param color
*/
public targetsChangeColor(color: number) {
this._targetsLastValue = this._targetsValue;
this._targetsValue = color;
}
/**
* 设置改变时间
* @param time
*/
public colorChange(time: number) {
Laya.Tween.to(ColorTool.getInstance(), { persent: 1 }, time * 1000, null, Laya.Handler.create(this, this.setPersent, [0]));
}
/**
* 颜色改变函数
* @param color 目标色值
* @param lastColor 当前色值
* @param persent 差值
*/
public colorFilter(color: number, lastColor: number, persent: number): Laya.ColorFilter {
let spliceColor = (color) => {
let result = { r: -1, g: -1, b: -1 };
result.b = color % 256;
result.g = Math.floor((color / 256)) % 256;
result.r = Math.floor((color / 256) / 256);
return result;
}
let result = spliceColor(color);
let lastResult = spliceColor(lastColor);
let colorMatrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
colorMatrix[0] = lastResult.r / 255 + (result.r - lastResult.r) / 255 * persent;
colorMatrix[6] = lastResult.g / 255 + (result.g - lastResult.g) / 255 * persent;
colorMatrix[12] = lastResult.b / 255 + (result.b - lastResult.b) / 255 * persent;
return new Laya.ColorFilter(colorMatrix);
}
public colorFilterNone(color: number) {
let spliceColor = (color) => {
let result = { r: -1, g: -1, b: -1 };
result.b = color % 256;
result.g = Math.floor((color / 256)) % 256;
result.r = Math.floor((color / 256) / 256);
return result;
}
let result = spliceColor(color);
let colorMatrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
colorMatrix[0] = result.r / 255;
colorMatrix[6] = result.g / 255;
colorMatrix[12] = result.b / 255;
return new Laya.ColorFilter(colorMatrix);
}
public set persent(persent: number) {
this._persent = persent;
let newTargetColorFilter = this.colorFilter(this._targetsValue, this._targetsLastValue, persent);
if (this.targets != null) {
for (let i = 0; i < this.targets.length; i++) {
if (this.targets[i] != null) {
this.targets[i].filters = [newTargetColorFilter];
}
}
}
}
public setPersent(persent: number) {
this._persent = persent;
}
public get persent(): number {
return this._persent;
}
/**
* 绿色滤镜
*/
public getGreenFilter(): Laya.ColorFilter {
return this.colorFilterNone(0x32CD32);
}
/**
* 灰色滤镜
*/
public getGrayFilter(): Laya.ColorFilter {
return this.colorFilterNone(0x6C7B8B);
}
/**
* 黑色滤镜
*/
public getBlackFilter(): Laya.ColorFilter {
return this.colorFilterNone(0x000000);
}
/**
* 红色滤镜
*/
public getRedFilter(): Laya.ColorFilter {
return this.colorFilterNone(0xFF0000);
}
/**
* 白色滤镜
*/
public getWhiteFilter(): Laya.ColorFilter {
return this.colorFilterNone(0xffffff);
}
public destroy() {
if (this.targets != null) {
this.targets = null;
}
ColorTool.instance = null;
}
}
}
用法 let sp:Laya.Sprite=new Laya.Sprite();
ColorTool.getInstance().addColorTarget(sp);
ColorTool.getInstance().targetsChangeColor(0x000000);
ColorTool.getInstance().colorChange(3)
主要思想, 差值算法 ,色值=(目标色值-起始色值)*百分比;
* leo
*/
module touch {
/**
* 颜色工具类
*/
export class ColorTool {
private _targetsValue: number = 16777215;
private _targetsLastValue: number = null;
private targets: Array<Laya.Sprite> = null;
public static instance: ColorTool = null;
private targetsFilter: Laya.ColorFilter = null;
private _persent: number = 0;
protected constructor() {
this.targets = new Array<Laya.Sprite>();
}
/**
* 实例
*/
public static getInstance(): ColorTool {
if (ColorTool.instance == null) {
ColorTool.instance = new ColorTool();
}
return ColorTool.instance;
}
/**
* 添加颜色改变对象
* @param target
*/
public addColorTarget(target: Laya.Sprite): void {
this.targets.push(target);
}
/**
* 设置颜色改变值
* @param color
*/
public targetsChangeColor(color: number) {
this._targetsLastValue = this._targetsValue;
this._targetsValue = color;
}
/**
* 设置改变时间
* @param time
*/
public colorChange(time: number) {
Laya.Tween.to(ColorTool.getInstance(), { persent: 1 }, time * 1000, null, Laya.Handler.create(this, this.setPersent, [0]));
}
/**
* 颜色改变函数
* @param color 目标色值
* @param lastColor 当前色值
* @param persent 差值
*/
public colorFilter(color: number, lastColor: number, persent: number): Laya.ColorFilter {
let spliceColor = (color) => {
let result = { r: -1, g: -1, b: -1 };
result.b = color % 256;
result.g = Math.floor((color / 256)) % 256;
result.r = Math.floor((color / 256) / 256);
return result;
}
let result = spliceColor(color);
let lastResult = spliceColor(lastColor);
let colorMatrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
colorMatrix[0] = lastResult.r / 255 + (result.r - lastResult.r) / 255 * persent;
colorMatrix[6] = lastResult.g / 255 + (result.g - lastResult.g) / 255 * persent;
colorMatrix[12] = lastResult.b / 255 + (result.b - lastResult.b) / 255 * persent;
return new Laya.ColorFilter(colorMatrix);
}
public colorFilterNone(color: number) {
let spliceColor = (color) => {
let result = { r: -1, g: -1, b: -1 };
result.b = color % 256;
result.g = Math.floor((color / 256)) % 256;
result.r = Math.floor((color / 256) / 256);
return result;
}
let result = spliceColor(color);
let colorMatrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
colorMatrix[0] = result.r / 255;
colorMatrix[6] = result.g / 255;
colorMatrix[12] = result.b / 255;
return new Laya.ColorFilter(colorMatrix);
}
public set persent(persent: number) {
this._persent = persent;
let newTargetColorFilter = this.colorFilter(this._targetsValue, this._targetsLastValue, persent);
if (this.targets != null) {
for (let i = 0; i < this.targets.length; i++) {
if (this.targets[i] != null) {
this.targets[i].filters = [newTargetColorFilter];
}
}
}
}
public setPersent(persent: number) {
this._persent = persent;
}
public get persent(): number {
return this._persent;
}
/**
* 绿色滤镜
*/
public getGreenFilter(): Laya.ColorFilter {
return this.colorFilterNone(0x32CD32);
}
/**
* 灰色滤镜
*/
public getGrayFilter(): Laya.ColorFilter {
return this.colorFilterNone(0x6C7B8B);
}
/**
* 黑色滤镜
*/
public getBlackFilter(): Laya.ColorFilter {
return this.colorFilterNone(0x000000);
}
/**
* 红色滤镜
*/
public getRedFilter(): Laya.ColorFilter {
return this.colorFilterNone(0xFF0000);
}
/**
* 白色滤镜
*/
public getWhiteFilter(): Laya.ColorFilter {
return this.colorFilterNone(0xffffff);
}
public destroy() {
if (this.targets != null) {
this.targets = null;
}
ColorTool.instance = null;
}
}
}
用法 let sp:Laya.Sprite=new Laya.Sprite();
ColorTool.getInstance().addColorTarget(sp);
ColorTool.getInstance().targetsChangeColor(0x000000);
ColorTool.getInstance().colorChange(3)
主要思想, 差值算法 ,色值=(目标色值-起始色值)*百分比;
没有找到相关结果
已邀请:
要回复问题请先登录
1 个回复
赞同来自: