Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a comment
- // uncomment the line below if you want to write a filterscript
- #define FILTERSCRIPT
- #include <a_samp>
- #define YSI_TESTS
- #include <YSI_Core\y_testing>
- // "Interpolation" is the technical name for what you are doing here.
- #define InterpolateColor InterpolateColour
- stock InterpolateColour(startcolor, endcolor, value, maxvalue, minvalue = 0)
- {
- if (value >= maxvalue) return endcolor;
- if (value <= minvalue) return startcolor;
- static r, g, b, a;
- new
- time = maxvalue - minvalue,
- stage = value - minvalue;
- return
- // Step 1: Get the starting colour components.
- r = startcolor >>> 24 ,
- g = startcolor >>> 16 & 0xFF,
- b = startcolor >>> 8 & 0xFF,
- a = startcolor & 0xFF,
- // Step 2: Interpolate between the end points, and add to the start.
- r += ((endcolor >>> 24 ) - r) * stage / time,
- g += ((endcolor >>> 16 & 0xFF) - g) * stage / time,
- b += ((endcolor >>> 8 & 0xFF) - b) * stage / time,
- a += ((endcolor & 0xFF) - a) * stage / time,
- // Step 3: Combine the individual components.
- (r << 24) | ((g & 0xFF) << 16) | ((b & 0xFF) << 8) | (a & 0xFF);
- }
- stock ColorFromValue(startcolor, endcolor, value, maxvalue, minvalue = 0)
- {
- if(value >= maxvalue) return endcolor;
- if(value <= minvalue) return startcolor;
- new Float:finalvalue = floatabs(floatdiv(float(value), floatsub(float(maxvalue), float(minvalue))));
- return (((floatround(floatmul(finalvalue, floatabs(floatadd(float((startcolor >> 24) & 0xFF), float((endcolor >> 24) & 0xFF))))) & 0xFF) << 24)
- | ((floatround(floatmul(finalvalue, floatabs(floatadd(float((startcolor >> 16) & 0xFF), float((endcolor >> 16) & 0xFF))))) & 0xFF) << 16)
- | ((floatround(floatmul(finalvalue, floatabs(floatadd(float((startcolor >> 8) & 0xFF), float((endcolor >> 8) & 0xFF))))) & 0xFF) << 8)
- | ((floatround(floatmul(finalvalue, floatabs(floatadd(float(startcolor & 0xFF), float(endcolor & 0xFF))))) & 0xFF)));
- }
- public OnFilterScriptInit()
- {
- print("\n----------------------------------");
- print(" Colour Interpolation Comparisons");
- print("----------------------------------\n");
- }
- Test:Interpolation()
- {
- ASSERT(InterpolateColour(0, 2, 1, 2, 0) == 1);
- ASSERT(InterpolateColour(0x00100020, 0x005000A0, 1, 4, 0) == 0x00200040);
- ASSERT(InterpolateColour(0x00100020, 0x005000A0, 7, 10, 6) == 0x00200040);
- ASSERT(InterpolateColour(0x005000A0, 0x00100020, 1, 4, 0) == 0x00400080);
- ASSERT(InterpolateColour(0x005000A0, 0x00100020, 7, 10, 6) == 0x00400080);
- ASSERT(InterpolateColour(0x00100020, 0x005000A0, 11, 4, 0) == 0x005000A0);
- ASSERT(InterpolateColour(0x00100020, 0x005000A0, -2, 4, 0) == 0x00100020);
- //printf("0x%08x", InterpolateColour(0x00100020, 0x00400080, 1, 4, 0));
- new t0, t1, t2;
- t0 = GetTickCount();
- for (new i = 0; i != 10000000; ++i)
- {
- ColorFromValue(0x34FD1102, 0x1199AABB, 3, 99, 1);
- }
- t1 = GetTickCount();
- for (new i = 0; i != 10000000; ++i)
- {
- InterpolateColour(0x34FD1102, 0x1199AABB, 3, 99, 1);
- }
- t2 = GetTickCount();
- printf("%d %d", t1 - t0, t2 - t1);
- }
Advertisement
Add Comment
Please, Sign In to add comment