Y_Less

Colour Interpolation Comparisons

Dec 6th, 2013
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.05 KB | None | 0 0
  1. // This is a comment
  2. // uncomment the line below if you want to write a filterscript
  3. #define FILTERSCRIPT
  4.  
  5. #include <a_samp>
  6.  
  7. #define YSI_TESTS
  8. #include <YSI_Core\y_testing>
  9.  
  10. // "Interpolation" is the technical name for what you are doing here.
  11. #define InterpolateColor InterpolateColour
  12. stock InterpolateColour(startcolor, endcolor, value, maxvalue, minvalue = 0)
  13. {
  14.     if (value >= maxvalue) return endcolor;
  15.     if (value <= minvalue) return startcolor;
  16.     static r, g, b, a;
  17.     new
  18.         time  = maxvalue - minvalue,
  19.         stage = value    - minvalue;
  20.     return
  21.         // Step 1:  Get the starting colour components.
  22.         r = startcolor >>> 24       ,
  23.         g = startcolor >>> 16 & 0xFF,
  24.         b = startcolor >>> 8  & 0xFF,
  25.         a = startcolor        & 0xFF,
  26.         // Step 2:  Interpolate between the end points, and add to the start.
  27.         r += ((endcolor >>> 24       ) - r) * stage / time,
  28.         g += ((endcolor >>> 16 & 0xFF) - g) * stage / time,
  29.         b += ((endcolor >>> 8  & 0xFF) - b) * stage / time,
  30.         a += ((endcolor        & 0xFF) - a) * stage / time,
  31.         // Step 3:  Combine the individual components.
  32.         (r << 24) | ((g & 0xFF) << 16) | ((b & 0xFF) << 8) | (a & 0xFF);
  33. }
  34.  
  35. stock ColorFromValue(startcolor, endcolor, value, maxvalue, minvalue = 0)
  36. {
  37.     if(value >= maxvalue) return endcolor;
  38.     if(value <= minvalue) return startcolor;
  39.     new Float:finalvalue = floatabs(floatdiv(float(value), floatsub(float(maxvalue), float(minvalue))));
  40.     return (((floatround(floatmul(finalvalue, floatabs(floatadd(float((startcolor >> 24) & 0xFF), float((endcolor >> 24) & 0xFF))))) & 0xFF) << 24)
  41.     | ((floatround(floatmul(finalvalue, floatabs(floatadd(float((startcolor >> 16) & 0xFF), float((endcolor >> 16) & 0xFF))))) & 0xFF) << 16)
  42.     | ((floatround(floatmul(finalvalue, floatabs(floatadd(float((startcolor >> 8) & 0xFF), float((endcolor >> 8) & 0xFF))))) & 0xFF) << 8)
  43.     | ((floatround(floatmul(finalvalue, floatabs(floatadd(float(startcolor & 0xFF), float(endcolor & 0xFF))))) & 0xFF)));
  44. }
  45.  
  46. public OnFilterScriptInit()
  47. {
  48.     print("\n----------------------------------");
  49.     print(" Colour Interpolation Comparisons");
  50.     print("----------------------------------\n");
  51. }
  52.  
  53. Test:Interpolation()
  54. {
  55.     ASSERT(InterpolateColour(0, 2, 1, 2, 0) == 1);
  56.     ASSERT(InterpolateColour(0x00100020, 0x005000A0, 1, 4,  0) == 0x00200040);
  57.     ASSERT(InterpolateColour(0x00100020, 0x005000A0, 7, 10, 6) == 0x00200040);
  58.     ASSERT(InterpolateColour(0x005000A0, 0x00100020, 1, 4,  0) == 0x00400080);
  59.     ASSERT(InterpolateColour(0x005000A0, 0x00100020, 7, 10, 6) == 0x00400080);
  60.     ASSERT(InterpolateColour(0x00100020, 0x005000A0, 11, 4, 0) == 0x005000A0);
  61.     ASSERT(InterpolateColour(0x00100020, 0x005000A0, -2, 4, 0) == 0x00100020);
  62.     //printf("0x%08x", InterpolateColour(0x00100020, 0x00400080, 1, 4,  0));
  63.     new t0, t1, t2;
  64.     t0 = GetTickCount();
  65.     for (new i = 0; i != 10000000; ++i)
  66.     {
  67.         ColorFromValue(0x34FD1102, 0x1199AABB, 3, 99, 1);
  68.     }
  69.     t1 = GetTickCount();
  70.     for (new i = 0; i != 10000000; ++i)
  71.     {
  72.         InterpolateColour(0x34FD1102, 0x1199AABB, 3, 99, 1);
  73.     }
  74.     t2 = GetTickCount();
  75.     printf("%d %d", t1 - t0, t2 - t1);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment