Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for a diagonal non-linear gradient

I'm looking for an algorithm to generate the following image:

Desired output

I'm only interested in the data shown in the upper left half (diagonal) of the square.


I'm currently at a starting point where I have the following simple linear function:

double GetColorAt(double x, double y)
{
   return 1 - (x + y);
}

where x runs from left to right and y from top to bottom in the ranges of [0...1]. The return value is a color component for RGB channels in the range of [0...1]. It generates me this:

Current output

like image 585
Jaanus Varus Avatar asked Dec 04 '25 14:12

Jaanus Varus


2 Answers

You lucked out, this is the sort of puzzle that I can't resist.

Analyzing the image, two things leap out.

  • r = g = b, i.e. this is a pure grayscale image.
  • r + a = 255.

This simplifies things greatly, it means you only need to analyze one channel to get the full characterization of the gradient.

Here's a plot of the R values from line 0 and line 128 of the image:

enter image description here

This plot greatly resembles the arc of a bullet as it's pulled down by gravity. So I plugged the basic equation into Excel and asked it to solve for the closest match to line 0 for 255 - (a*(b*x)^2) for varying vales of a and b. It came up with values of a=6.998191873 and b=0.023556823. Those values also closely tracked the ones from line 128 when I substituted x/2, so I knew I was on the right track.

Here's the algorithm in Python:

for y in range(height):
    scale = (256 - y) / 256.0
    for x in range(width):
        r = int(round(255 - (6.998191873 * (0.023556823 * x/scale) ** 2)))
        r = max(r, 0)
        ld2[x, y] = (r, r, r, 255-r)

It's not quite exact, which is most noticeable at the bottom of the image where it seems to be a pixel off. But it's visually identical.

enter image description here

like image 76
Mark Ransom Avatar answered Dec 07 '25 17:12

Mark Ransom


If anyone likes testing their theory of how to generate the gradient by looking at hex rather than the image, the following may help. It is resized down by a factor of 10 to 25px by 25px.

0000000: ff fe fd fa f7 f3 ed e8 e1 d9 d1 c8 be b2 a7 9a 8c 7e 6e 5e 4d 3c 2a 12 18  .................~n^M<*..
0000019: ff fe fc fa f6 f1 ec e6 de d6 cd c3 b8 ac 9f 91 83 73 62 51 3e 2c 15 08 1e  .................sbQ>,...
0000032: ff fe fc f9 f5 f0 ea e3 db d2 c8 bd b1 a4 96 87 77 66 54 41 2d 17 02 06 20  ................wfTA-... 
000004b: ff fe fc f9 f4 ef e8 e1 d8 ce c3 b7 aa 9c 8c 7c 6a 58 44 2f 18 04 00 06 20  ...............|jXD/.... 
0000064: ff fe fb f8 f3 ed e6 de d4 c9 bd b0 a1 92 81 6f 5b 47 31 1a 04 00 00 06 20  ...............o[G1..... 
000007d: ff fe fb f7 f2 eb e3 da cf c4 b6 a8 98 86 74 60 4a 34 1b 04 00 01 00 06 20  ..............t`J4...... 
0000096: ff fe fb f6 f0 e9 e0 d6 ca bd ae 9e 8c 79 64 4e 37 1c 05 00 00 00 00 06 20  .............ydN7....... 
00000af: ff fd fa f5 ef e7 dd d1 c4 b5 a5 93 7f 69 52 3a 1d 03 00 00 00 00 00 06 20  .............iR:........ 
00000c8: ff fd f9 f4 ed e4 d9 cc bd ac 9a 85 6f 57 3d 20 05 00 00 00 00 00 00 06 20  ............oW= ........ 
00000e1: ff fd f9 f3 ea e0 d3 c5 b4 a1 8c 75 5c 41 22 05 00 00 00 00 00 00 00 06 20  ...........u\A"......... 
00000fa: ff fd f8 f1 e7 dc cd bd a9 94 7c 61 45 25 06 00 00 00 00 00 00 00 00 06 20  ..........|aE%.......... 
0000113: ff fc f7 ef e4 d6 c6 b2 9c 84 68 4a 28 07 00 00 00 00 00 00 00 00 00 06 20  ..........hJ(........... 
000012c: ff fc f5 ec df cf bc a6 8c 6f 4f 2b 08 00 00 00 00 00 00 00 00 00 00 06 20  .........oO+............ 
0000145: ff fb f4 e9 da c7 b0 96 77 56 2e 08 00 00 00 00 00 00 00 00 00 00 00 06 20  ........wV.............. 
000015e: ff fa f1 e4 d2 bc a1 81 5d 33 09 00 01 00 00 00 00 00 00 00 00 00 00 06 20  ........]3.............. 
0000177: fe f9 ee de c8 ad 8c 66 38 0a 00 01 00 00 00 00 00 00 00 00 00 00 00 06 20  .......f8............... 
0000190: fe f8 ea d6 bb 99 70 3f 0c 00 01 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ......p?................ 
00001a9: fe f6 e5 cb a8 7d 47 0d 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 06 1f  .....}G..................
00001c2: fe f3 dc ba 8d 51 0f 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  .....Q.................. 
00001db: fd ee ce a0 5e 12 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ....^................... 
00001f4: fc e6 b9 70 16 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 1f  ...p.....................
000020d: fa d9 89 1d 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ........................ 
0000226: f9 b4 26 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  ..&..................... 
000023f: e6 40 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  .@...................... 
0000258: 74 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 20  t....................... 
like image 30
Mark Setchell Avatar answered Dec 07 '25 17:12

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!