Hey /sci/,
I'm trying to program a function that simply takes an integer, but cycles through all non-grey RGB values (i.e. one value is 255, one value is 0, one changes). My current method requires a lot of if-statements, so I was wondering if there was a mathematical approach I could take. The value of each color's brightness has a trapezoidal waveform, and so far I have this function (couldn't get latex to work for some reason so bare with me):
(255/pi) * [ sin^-1( sin( pi/2 * x ) ) + cos^-1( cos( pi/2*x ) ) ]
However I want to "stretch" just the horizontal parts so they last for a longer interval (picture is a visual explanation), so that the values read as follows:
x=0, y=0
1 <= x <= 3. y=255
4 <= x <= 6, y=0
Any ideas how to achieve this mathematically, without resorting to piece-wise?
>>7754984
Op here
To clarify, I want to preserve the characteristic that the wave goes from 0 to 255 in an interval of 1, or [x,x+1], so normal sinusoidal stretching won't suffice.
>>7754984
Pre-generate a lookup table
>>7754984
Let R3 represent an RGB space.
Imagine an object flying around in an orbit.
The position of that object marks your RGB value.
Why not something like
min( 255, K - L * | 255 * (x-offset) | )
where you adjust the offset, K and L to your liking? Deep down, using min and abs is obviously the same as nested if-statements, but that'd still yield a simple formula.
>>7756650
yes, here is an extended version
https://www.desmos.com/calculator/cndmbbi3qa
(One uses mod and the other arcsin(sin(x)), I dont know which one is more efficient)
>>7754984
Just use the HSV color space and convert back to RGB.
>>7754984
Matlab can realise yo dreams homie!!
What's the point of using asin(sin(x)), asin is just inverse of sin, so wouldn't it just return it's argument?
>>7759666
Depends on which interval you work. asin always returns values between -pi/2 and pi/2 and pi/2*x obviously doesn't. In the interval -1 to 1, it's the same. But outside that, pi/2*x is a straight line and asin(sin(pi/2*x)) is a triangle.