Skip to content

Commit 4081a9f

Browse files
Improve FlxSprite color transform handling, deprecate FlxColor.to24Bit() (#3413)
* deprecate color.to24Bit() * improve FlxSprite color transform handling * bound alpha earlier * fix FlxText color * make colorTransform final * deprecate useColorTransform, add hasColorTransform() * revert setting colorTransform to final, for now * bind alpha everywhere + doc --------- Co-authored-by: GeoKureli-BlackbookPro <GKurelic@gmail.com>
1 parent 199db02 commit 4081a9f

File tree

9 files changed

+95
-82
lines changed

9 files changed

+95
-82
lines changed

flixel/FlxSprite.hx

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ class FlxSprite extends FlxObject
208208
public var bakedRotationAngle(default, null):Float = 0;
209209

210210
/**
211-
* Set alpha to a number between `0` and `1` to change the opacity of the sprite.
211+
* Set alpha to a number between `0` and `1` to change the opacity of the sprite. Calling
212+
* `setColorTransform` will also change this value
213+
*
214+
* **NOTE:** This value is automatically clamped to 0 <= a <= 1
212215
@see https://snippets.haxeflixel.com/sprites/alpha/
213216
*/
214217
public var alpha(default, set):Float = 1.0;
@@ -246,7 +249,7 @@ class FlxSprite extends FlxObject
246249
/**
247250
* Change the size of your sprite's graphic.
248251
* NOTE: The hitbox is not automatically adjusted, use `updateHitbox()` for that.
249-
* WARNING: With `FlxG.renderBlit`, scaling sprites decreases rendering performance by a factor of about x10!
252+
* **WARNING:** With `FlxG.renderBlit`, scaling sprites decreases rendering performance by a factor of about x10!
250253
* @see https://snippets.haxeflixel.com/sprites/scale/
251254
*/
252255
public var scale(default, null):FlxPoint;
@@ -255,19 +258,23 @@ class FlxSprite extends FlxObject
255258
* Blending modes, just like Photoshop or whatever, e.g. "multiply", "screen", etc.
256259
*/
257260
public var blend(default, set):BlendMode;
258-
261+
259262
/**
260-
* Tints the whole sprite to a color (`0xRRGGBB` format) - similar to OpenGL vertex colors. You can use
261-
* `0xAARRGGBB` colors, but the alpha value will simply be ignored. To change the opacity use `alpha`.
263+
* Multiplies this sprite's image by the given red, green and blue components, alpha is ignored.
264+
* To change the opacity use `alpha`. Calling `setColorTransform` will also change this value.
262265
* @see https://snippets.haxeflixel.com/sprites/color/
263266
*/
264-
public var color(default, set):FlxColor = 0xffffff;
265-
266-
public var colorTransform(default, null):ColorTransform;
267+
public var color(default, set):FlxColor = FlxColor.WHITE;
268+
269+
/**
270+
* The color effects of this sprite, changes to `color` or `alplha` will be reflected here
271+
*/
272+
public var colorTransform(default, null) = new ColorTransform();
267273

268274
/**
269275
* Whether or not to use a `ColorTransform` set via `setColorTransform()`.
270276
*/
277+
@:deprecated("useColorTransform is deprecated, use hasColorTransform(), instead")// 6.1.0
271278
public var useColorTransform(default, null):Bool = false;
272279

273280
/**
@@ -394,7 +401,6 @@ class FlxSprite extends FlxObject
394401
scale = FlxPoint.get(1, 1);
395402
_halfSize = FlxPoint.get();
396403
_matrix = new FlxMatrix();
397-
colorTransform = new ColorTransform();
398404
_scaledOrigin = new FlxPoint();
399405
}
400406

@@ -428,7 +434,6 @@ class FlxSprite extends FlxObject
428434
_flashRect2 = null;
429435
_flashPointZero = null;
430436
_matrix = null;
431-
colorTransform = null;
432437
blend = null;
433438

434439
frames = null;
@@ -1008,7 +1013,7 @@ class FlxSprite extends FlxObject
10081013
dirty = true;
10091014
return positions;
10101015
}
1011-
1016+
10121017
/**
10131018
* Sets the sprite's color transformation with control over color offsets.
10141019
* With `FlxG.renderTile`, offsets are only supported on OpenFL Next version 3.6.0 or higher.
@@ -1022,33 +1027,48 @@ class FlxSprite extends FlxObject
10221027
* @param blueOffset The offset for the blue color channel value, in the range from `-255` to `255`.
10231028
* @param alphaOffset The offset for alpha transparency channel value, in the range from `-255` to `255`.
10241029
*/
1030+
@:haxe.warning("-WDeprecated")
10251031
public function setColorTransform(redMultiplier = 1.0, greenMultiplier = 1.0, blueMultiplier = 1.0, alphaMultiplier = 1.0,
10261032
redOffset = 0.0, greenOffset = 0.0, blueOffset = 0.0, alphaOffset = 0.0):Void
10271033
{
1028-
color = FlxColor.fromRGBFloat(redMultiplier, greenMultiplier, blueMultiplier).to24Bit();
1029-
alpha = alphaMultiplier;
1030-
1034+
alphaMultiplier = FlxMath.bound(alphaMultiplier, 0, 1);
1035+
@:bypassAccessor color = FlxColor.fromRGBFloat(redMultiplier, greenMultiplier, blueMultiplier, 1.0);
1036+
@:bypassAccessor alpha = alphaMultiplier;
1037+
10311038
colorTransform.setMultipliers(redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier);
10321039
colorTransform.setOffsets(redOffset, greenOffset, blueOffset, alphaOffset);
1033-
1034-
useColorTransform = alpha != 1 || color != 0xffffff || colorTransform.hasRGBOffsets();
1040+
useColorTransform = hasColorTransformRaw();
1041+
10351042
dirty = true;
10361043
}
10371044

1045+
@:haxe.warning("-WDeprecated")
10381046
function updateColorTransform():Void
10391047
{
1040-
if (colorTransform == null)
1041-
return;
1042-
1043-
useColorTransform = alpha != 1 || color != 0xffffff;
1044-
if (useColorTransform)
1045-
colorTransform.setMultipliers(color.redFloat, color.greenFloat, color.blueFloat, alpha);
1046-
else
1047-
colorTransform.setMultipliers(1, 1, 1, 1);
1048-
1048+
colorTransform.setMultipliers(color.redFloat, color.greenFloat, color.blueFloat, alpha);
1049+
useColorTransform = hasColorTransformRaw();
1050+
10491051
dirty = true;
10501052
}
1051-
1053+
1054+
/**
1055+
* Whether this sprite has a color transform, menaing any of the following: less than full
1056+
* `alpha`, a `color` tint, or a `colorTransform` whos values are not the default.
1057+
*/
1058+
@:haxe.warning("-WDeprecated")
1059+
public function hasColorTransform()
1060+
{
1061+
return useColorTransform || hasColorTransformRaw();
1062+
}
1063+
1064+
/**
1065+
* Helper for the non-deprecated component of `hasColorTransform`
1066+
*/
1067+
function hasColorTransformRaw()
1068+
{
1069+
return alpha != 1 || color.rgb != 0xffffff || colorTransform.hasRGBAOffsets();
1070+
}
1071+
10521072
/**
10531073
* Checks to see if a point in 2D world space overlaps this `FlxSprite` object's
10541074
* current displayed pixels. This check is ALWAYS made in screen space, and
@@ -1209,18 +1229,18 @@ class FlxSprite extends FlxObject
12091229
{
12101230
if (_frame == null || !dirty)
12111231
return framePixels;
1212-
1232+
12131233
// don't try to regenerate frame pixels if _frame already uses it as source of graphics
12141234
// if you'll try then it will clear framePixels and you won't see anything
12151235
if (FlxG.renderTile && _frameGraphic != null)
12161236
{
12171237
dirty = false;
12181238
return framePixels;
12191239
}
1220-
1221-
var doFlipX:Bool = checkFlipX();
1222-
var doFlipY:Bool = checkFlipY();
1223-
1240+
1241+
final doFlipX = checkFlipX();
1242+
final doFlipY = checkFlipY();
1243+
12241244
if (!doFlipX && !doFlipY && _frame.type == FlxFrameType.REGULAR)
12251245
{
12261246
framePixels = _frame.paint(framePixels, _flashPointZero, false, true);
@@ -1229,20 +1249,20 @@ class FlxSprite extends FlxObject
12291249
{
12301250
framePixels = _frame.paintRotatedAndFlipped(framePixels, _flashPointZero, FlxFrameAngle.ANGLE_0, doFlipX, doFlipY, false, true);
12311251
}
1232-
1233-
if (useColorTransform)
1252+
1253+
if (FlxG.renderBlit && hasColorTransform())
12341254
{
12351255
framePixels.colorTransform(_flashRect, colorTransform);
12361256
}
1237-
1257+
12381258
if (FlxG.renderTile && useFramePixels)
12391259
{
12401260
// recreate _frame for native target, so it will use modified framePixels
12411261
_frameGraphic = FlxDestroyUtil.destroy(_frameGraphic);
12421262
_frameGraphic = FlxGraphic.fromBitmapData(framePixels, false, null, false);
12431263
_frame = _frameGraphic.imageFrame.frame.copyTo(_frame);
12441264
}
1245-
1265+
12461266
dirty = false;
12471267
return framePixels;
12481268
}
@@ -1504,25 +1524,24 @@ class FlxSprite extends FlxObject
15041524
}
15051525

15061526
@:noCompletion
1507-
function set_alpha(Alpha:Float):Float
1527+
function set_alpha(value:Float):Float
15081528
{
1509-
if (alpha == Alpha)
1510-
{
1511-
return Alpha;
1512-
}
1513-
alpha = FlxMath.bound(Alpha, 0, 1);
1529+
value = FlxMath.bound(value, 0, 1);
1530+
if (alpha == value)
1531+
return value;
1532+
1533+
alpha = value;
15141534
updateColorTransform();
15151535
return alpha;
15161536
}
15171537

15181538
@:noCompletion
1519-
function set_color(Color:FlxColor):Int
1539+
function set_color(value:FlxColor):Int
15201540
{
1521-
if (color == Color)
1522-
{
1523-
return Color;
1524-
}
1525-
color = Color;
1541+
if (color == value)
1542+
return value;
1543+
1544+
color = value;
15261545
updateColorTransform();
15271546
return color;
15281547
}

flixel/system/debug/DebuggerUtil.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DebuggerUtil
3030
tf.antiAliasType = AntiAliasType.NORMAL;
3131
tf.gridFitType = GridFitType.PIXEL;
3232
#end
33-
tf.defaultTextFormat = new TextFormat(FlxAssets.FONT_DEBUGGER, Size, Color.to24Bit());
33+
tf.defaultTextFormat = new TextFormat(FlxAssets.FONT_DEBUGGER, Size, Color.rgb);
3434
tf.alpha = Color.alphaFloat;
3535
tf.autoSize = TextFieldAutoSize.LEFT;
3636
return tf;

flixel/system/frontEnds/CameraFrontEnd.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class CameraFrontEnd
289289
}
290290
else
291291
{
292-
camera.fill(camera.bgColor.to24Bit(), camera.useBgAlphaBlending, camera.bgColor.alphaFloat);
292+
camera.fill(camera.bgColor.rgb, camera.useBgAlphaBlending, camera.bgColor.alphaFloat);
293293
}
294294
}
295295
}

flixel/text/FlxText.hx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -664,17 +664,24 @@ class FlxText extends FlxSprite
664664
updateDefaultFormat();
665665
return LetterSpacing;
666666
}
667+
668+
override function setColorTransform(redMultiplier = 1.0, greenMultiplier = 1.0, blueMultiplier = 1.0, alphaMultiplier = 1.0, redOffset = 0.0, greenOffset = 0.0, blueOffset = 0.0, alphaOffset = 0.0)
669+
{
670+
super.setColorTransform(1, 1, 1, 1, redOffset, greenOffset, blueOffset, alphaOffset);
671+
_defaultFormat.color = FlxColor.fromRGBFloat(redMultiplier, greenMultiplier, blueMultiplier, 0);
672+
updateDefaultFormat();
673+
}
667674

668-
override function set_color(Color:FlxColor):Int
675+
override function set_color(value:FlxColor):Int
669676
{
670-
if (_defaultFormat.color == Color.to24Bit())
677+
if (_defaultFormat.color == value.rgb)
671678
{
672-
return Color;
679+
return value;
673680
}
674-
_defaultFormat.color = Color.to24Bit();
675-
color = Color;
681+
_defaultFormat.color = value.rgb;
682+
color = value;
676683
updateDefaultFormat();
677-
return Color;
684+
return value;
678685
}
679686

680687
inline function get_font():String
@@ -855,19 +862,7 @@ class FlxText extends FlxSprite
855862

856863
override function updateColorTransform():Void
857864
{
858-
if (colorTransform == null)
859-
colorTransform = new ColorTransform();
860-
861-
if (alpha != 1)
862-
{
863-
colorTransform.alphaMultiplier = alpha;
864-
useColorTransform = true;
865-
}
866-
else
867-
{
868-
colorTransform.alphaMultiplier = 1;
869-
useColorTransform = false;
870-
}
865+
colorTransform.alphaMultiplier = alpha;
871866

872867
dirty = true;
873868
}
@@ -1232,7 +1227,7 @@ class FlxText extends FlxSprite
12321227
{
12331228
// Apply the default format
12341229
copyTextFormat(_defaultFormat, FormatAdjusted, false);
1235-
FormatAdjusted.color = UseBorderColor ? borderColor.to24Bit() : _defaultFormat.color;
1230+
FormatAdjusted.color = UseBorderColor ? borderColor.rgb : _defaultFormat.color;
12361231
textField.setTextFormat(FormatAdjusted);
12371232

12381233
// Apply other formats
@@ -1247,7 +1242,7 @@ class FlxText extends FlxSprite
12471242
{
12481243
var textFormat:TextFormat = formatRange.format.format;
12491244
copyTextFormat(textFormat, FormatAdjusted, false);
1250-
FormatAdjusted.color = UseBorderColor ? formatRange.format.borderColor.to24Bit() : textFormat.color;
1245+
FormatAdjusted.color = UseBorderColor ? formatRange.format.borderColor.rgb : textFormat.color;
12511246
}
12521247

12531248
textField.setTextFormat(FormatAdjusted, formatRange.range.start, Std.int(Math.min(formatRange.range.end, textField.text.length)));

flixel/util/FlxBitmapDataUtil.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class FlxBitmapDataUtil
200200
{
201201
identical = false;
202202

203-
if (pixel1.to24Bit() != pixel2.to24Bit())
203+
if (pixel1.rgb != pixel2.rgb)
204204
{
205205
result.setPixel32(i, j,
206206
FlxColor.fromRGB(getDiff(pixel1.red, pixel2.red), getDiff(pixel1.green, pixel2.green), getDiff(pixel1.blue, pixel2.blue)));

flixel/util/FlxColor.hx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package flixel.util;
22

3+
import flixel.tweens.FlxEase;
34
import flixel.math.FlxMath;
45
import flixel.system.macros.FlxMacroUtil;
56

@@ -258,16 +259,13 @@ abstract FlxColor(Int) from Int from UInt to Int to UInt
258259
* @param Ease An optional easing function, such as those provided in FlxEase
259260
* @return An array of colors of length Steps, shifting from Color1 to Color2
260261
*/
261-
public static function gradient(Color1:FlxColor, Color2:FlxColor, Steps:Int, ?Ease:Float->Float):Array<FlxColor>
262+
public static function gradient(Color1:FlxColor, Color2:FlxColor, Steps:Int, ?Ease:EaseFunction):Array<FlxColor>
262263
{
263264
var output = new Array<FlxColor>();
264265

265266
if (Ease == null)
266267
{
267-
Ease = function(t:Float):Float
268-
{
269-
return t;
270-
}
268+
Ease = FlxEase.linear;
271269
}
272270

273271
for (step in 0...Steps)
@@ -366,6 +364,7 @@ abstract FlxColor(Int) from Int from UInt to Int to UInt
366364
*
367365
* @return A 24 bit version of this color
368366
*/
367+
@:deprecated("to24Bit() is deprecated, use rgb field, instead.")
369368
public inline function to24Bit():FlxColor
370369
{
371370
return this & 0xffffff;

flixel/util/FlxSpriteUtil.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class FlxSpriteUtil
572572

573573
if (FillColor != FlxColor.TRANSPARENT)
574574
{
575-
flashGfx.beginFill(FillColor.to24Bit(), FillColor.alphaFloat);
575+
flashGfx.beginFill(FillColor.rgb, FillColor.alphaFloat);
576576
}
577577
}
578578

@@ -634,7 +634,7 @@ class FlxSpriteUtil
634634
if (lineStyle.miterLimit == null)
635635
lineStyle.miterLimit = 3;
636636

637-
flashGfx.lineStyle(lineStyle.thickness, color.to24Bit(), color.alphaFloat, lineStyle.pixelHinting, lineStyle.scaleMode, lineStyle.capsStyle,
637+
flashGfx.lineStyle(lineStyle.thickness, color.rgb, color.alphaFloat, lineStyle.pixelHinting, lineStyle.scaleMode, lineStyle.capsStyle,
638638
lineStyle.jointStyle, lineStyle.miterLimit);
639639
}
640640
}

flixel/util/FlxStringUtil.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ class FlxStringUtil
492492
{
493493
for (i in 0...ColorMap.length)
494494
{
495-
ColorMap[i] = ColorMap[i].to24Bit();
495+
ColorMap[i] = ColorMap[i].rgb;
496496
}
497497
}
498498

0 commit comments

Comments
 (0)