GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2013-05-25 15:00:36

Miah_84
Member
Registered: 2013-05-23
Posts: 14

HSV_shift

Expand/*
Original script by Miah_84. This script may be used or modified freely, no attribution needed.

HSV_shift(CurrentColor,HueShift,SaturationShift,ValueShift)

Description: Shifts current color using Hue,Saturation,Value shifts

Returns: A shifted color

Example: ShiftedColor = HSV_shift(c_red,201,97,64)

argument0 = color to be shifted
argument1 = hue shift
argument2 = saturation shift
argument3 = value shift
*/

c = argument0;
h = argument1;
s = argument2;
v = argument3;

new_c = make_color_hsv((color_get_hue(c)+h) & 255, (color_get_saturation(c)+s) & 255, (color_get_value(c)+v) & 255)

return new_c;

Last edited by Miah_84 (2013-05-26 10:22:10)

Offline

#2 2013-05-25 16:49:38

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: HSV_shift

Good idea for a script. Thanks!

There are a few things to consider here.

First, the modulus operand should be 256 to cover the entire range of accepted values correctly.

Second, in the GM order of operations, the modulus operator takes precedence over addition. The addition should be enclosed in parentheses to ensure it is carried out before the modulus.

Third, negative values will still be negative after the modulus is applied. It would take (((n mod 256) + 256) mod 256) to remap any given value of n into the 0-255 range.

Fourth, older versions of Game Maker and the GM:Studio Windows runner handle out of range values in the way you want already. But the HTML5 runner does not and I'm unable to test other platforms, so that's a problem. I'd love it if all of the runners handled make_color_hsv() the same but I doubt this 'fix' would be considered since it is reliant on undefined behavior.

All that said, because the range we are dealing with is a power of two, (n & 255) will achieve the results we want without using the modulus operator at all. Even though the order of operations should ensure the bitwise AND operator is executed after addition, the addition should be placed in parentheses for clarity.

Expandnew_c = make_color_hsv((color_get_hue(c)+h) & 255, (color_get_saturation(c)+s) & 255, (color_get_value(c)+v) & 255)

Abusing forum power since 1986.

Offline

#3 2013-05-25 23:04:27

Miah_84
Member
Registered: 2013-05-23
Posts: 14

Re: HSV_shift

I will update the code. thanks for the advice. I didn't realize GM already did this and also used the mod before the addition. I should have used parenthesis (I know better), but I made this on the fly for a bug report a while ago while i was at work (i made it on my phone.) I should have looked over it again before posting it. either way, i will update the code. thanks xot

Offline

#4 2013-08-21 18:03:46

brac37
Member
Registered: 2010-02-12
Posts: 18

Re: HSV_shift

The modulus operand for the hue is indeed 255. 0 and 255 are both accepted for red. 85 is green and 170 is blue. I do not know what GM does with out of range hue.

Last edited by brac37 (2013-08-21 18:05:31)

Offline

#5 2013-08-23 22:56:57

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: HSV_shift

Oh dear. You are absolutely correct about hue = 0 and hue = 255 being equivalent for all versions of GM (since v5), which is to say, for hue, [0..255] maps to [0..360 degrees]. I don't know why I assumed otherwise. I should know better than to make claims without testing them first.

Apologies for the bad advice in my previous post, Miah_84.

Thank you for the correction, brac37.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB