AS class: color property ColorProperty

Source: Internet
Author: User
Tags getcolor

Although it is not difficult to use AS to adjust the color, brightness, gray level, saturation, contrast, and inversion of an image, it is a little troublesome to use because it involves the color matrix of ColorMatrixFilter, therefore, the ColorProperty class is written.

This class extends the MovieClip class and adds these attributes to MovieClip:

Color: _ color
Brightness: _ brightness
Grayscale: _ grayscale
Saturation: _ saturation
Contrast: _ contrast
Reverse: _ invert
Of course, you can also rewrite this class to make it a new class, instead of extending the MovieClip class.

Usage (same as _ width, _ height ):

Import ColorProperty;
ColorProperty. init ();
// Color, such as 0xFF0000 hexadecimal
// Img. _ color = 0x333399;
// Trace (img. _ color );
// Brightness, value range:-255 ~ 255
Img. _ brightness = 100;
// Trace (img. _ brightness)
// Grayscale; Boolean value; true indicates grayscale; false indicates vice versa.
// Img. _ grayscale = true;
// Trace (img. _ grayscale );
// Saturation, generally ranging from 0 ~ 3.
// Img. _ saturation = 3;
// Trace (img. _ saturation );
// Contrast, value range: 0 ~ 1
// Img. _ contrast = 0.15;
// Reverse phase. boolean value. true indicates reverse phase. false indicates reverse phase.
// Trace (img. _ contrast );
// Img. _ invert = true;

The Code is as follows:

Copy codeThe Code is as follows :/**
* @ Name: ColorProperty (MovieClip color attribute)
* Color: _ color, brightness: _ brightness, Gray: _ grayscale, saturation: _ saturation, contrast: _ contrast, reverse: _ invert
* @ Author: Flashlizi
* @ Version: 1.0
*/
Import flash. filters. ColorMatrixFilter;
Class ColorProperty
{
// _ Matrix is the default constant matrix of the ColorMatrixFilter class.
// _ NRed, _ nGreen, _ nBlue is the constant of the brightness of computer graphics.
// Private static var _ matrix: Array = [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0];
Private static var _ nRed: Number = 0.3086;
Private static var _ nGreen: Number = 0.6094;
Private static var _ nBlue: Number = 0.0820;
Function ColorProperty ()
{
}
Public static function init ()
{
SetColorProperty ();
// Color
MovieClip. prototype. addProperty ("_ color", MovieClip. prototype. getColor, MovieClip. prototype. setColor );
// Brightness (value range:-255 ~ 255)
MovieClip. prototype. addProperty ("_ brightness", MovieClip. prototype. getBrightness, MovieClip. prototype. setBrightness );
// Grayscale
MovieClip. prototype. addProperty ("_ grayscale", MovieClip. prototype. getGrayscale, MovieClip. prototype. setGrayscale );
// Saturation (Saturation level generally ranges from 0 ~ 3)
MovieClip. prototype. addProperty ("_ saturation", MovieClip. prototype. getSaturation, MovieClip. prototype. setSaturation );
// Contrast (value range: 0 ~ 1)
MovieClip. prototype. addProperty ("_ contrast", MovieClip. prototype. getContrast, MovieClip. prototype. setContrast );
// Reverse-phase Invert
MovieClip. prototype. addProperty ("_ invert", MovieClip. prototype. getInvert, MovieClip. prototype. setInvert );
}
Private static function setColorProperty ()
{
// Color, getter & setter
MovieClip. prototype. getColor = function (): Number
{
Return MovieClip. prototype. _ color;
}
MovieClip. prototype. setColor = function (nColor: Number): Void
{
Var colorStr: String = nColor. toString (16 );
Var nRed: Number = Number ("0x" + colorStr. slice (0, 2 ));
Var nGreen: Number = Number ("0x" + colorStr. slice (2, 4 ));
Var nBlue: Number = Number ("0x" + colorStr. slice (4, 6 ));
Var Color_Matrix: Array = [1, 0, 0, 0, nRed, 0, 1, 0, 0, nGreen, 0, 0, 1, 0, nBlue, 0, 0, 0, 0, 1, 0];
This. filters = [new ColorMatrixFilter (Color_Matrix)];
MovieClip. prototype. _ color = nColor;
}
// Brightness, getter & setter
MovieClip. prototype. getBrightness = function (): Number
{
Return MovieClip. prototype. _ brightness;
}
MovieClip. prototype. setBrightness = function (offset: Number): Void
{
Var Brightness_Matrix: Array = [1, 0, 0, 0, offset, 0, 0, 1, 0, 0, offset, 0, 0, 1, 0, offset, 0, 0, 0, 1, 0];
This. filters = [new ColorMatrixFilter (Brightness_Matrix)];
MovieClip. prototype. _ brightness = offset;
}
// Grayscale, getter & setter
MovieClip. prototype. getGrayscale = function (): Boolean
{
Return MovieClip. prototype. _ grayscale;
}
MovieClip. prototype. setGrayscale = function (yes: Boolean): Void
{
If (yes)
{
Var Grayscale_Matrix: Array = [_ nRed, _ nGreen, _ nBlue, 0, 0, _ nRed, _ nGreen, _ nBlue, 0, 0, _ nRed, _ nGreen, _ nBlue, 0, 0, 0, 0, 0, 1, 0];
This. filters = [new ColorMatrixFilter (Grayscale_Matrix)];
MovieClip. prototype. _ grayscale = true;
} Else
{
MovieClip. prototype. _ grayscale = false;
}
}
// Saturation, getter & setter
MovieClip. prototype. getSaturation = function (): Number
{
Return MovieClip. prototype. _ saturation;
}
MovieClip. prototype. setSaturation = function (nLevel: Number): Void
{
Var srcRa: Number = (1-nLevel) * _ nRed + nLevel;
Var srcGa: Number = (1-nLevel) * _ nGreen;
Var srcBa: Number = (1-nLevel) * _ nBlue;
Var srcRb: Number = (1-nLevel) * _ nRed;
Var srcGb: Number = (1-nLevel) * _ nGreen + nLevel;
Var srcBb: Number = (1-nLevel) * _ nBlue;
Var srcRc: Number = (1-nLevel) * _ nRed;
Var srcGc: Number = (1-nLevel) * _ nGreen;
Var srcBc: Number = (1-nLevel) * _ nBlue + nLevel;
Var Saturation_Matrix: Array = [srcRa, srcGa, srcBa, 0, 0, srcRb, srcGb, srcBb, 0, 0, srcRc, srcGc, srcBc, 0, 0, 0, 0, 0, 1, 0];
This. filters = [new ColorMatrixFilter (Saturation_Matrix)];
MovieClip. prototype. _ saturation = nLevel;
}
// Contrast, getter & setter
MovieClip. prototype. getContrast = function (): Number
{
Return MovieClip. prototype. _ contrast;
}
MovieClip. prototype. setContrast = function (nLevel: Number): Void
{
Var Scale: Number = nLevel * 11;
Var Offset: Number = 63.5-(nLevel * 698.5 );
Var Contrast_Matrix: Array = [Scale, 0, 0, 0, Offset, 0, Scale, 0, 0, Offset, 0, 0, Scale, 0, Offset, 0, 0, 0, 1, 0];
This. filters = [new ColorMatrixFilter (Contrast_Matrix)];
MovieClip. prototype. _ contrast = nLevel;
}
// Reverse-phase Invert, getter & setter
MovieClip. prototype. getInvert = function (): Boolean
{
Return MovieClip. prototype. _ invert;
}
MovieClip. prototype. setInvert = function (yes: Boolean): Void
{
If (yes)
{
Var Invert_Matrix: Array = [-1, 0, 0, 0,255, 0,-1, 0, 0,255, 0,-1, 0,255, 0, 0, 0, 1, 0];
This. filters = [new ColorMatrixFilter (Invert_Matrix)];
MovieClip. prototype. _ invert = true;
} Else
{
MovieClip. prototype. _ invert = false;
}
}
}
}

Download: ColorProperty.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.