Error in color fading function - c

I found this old color fading function in my snippets folder and would like to implement it to one of my projects. It can be used to fade one color to another. It's a very long one-liner:
D3DCOLOR GetFadedColor(D3DCOLOR from, D3DCOLOR to, float factor)
{
return (factor<0.0f)?from:((factor>1.0f)?to:((((from>>24)>(to>>24))?((from>>24)-(D3DCOLOR)(factor*(float)((from>>24)-(to>>24)))):((from>>24)+(D3DCOLOR)(factor*(float)((to>>24)-(from>>24))))<<24)|((((from<<8)>>24)>((to<<8)>>24))?(((from<<8)>>24)-(D3DCOLOR)(factor*(float)(((from<<8)>>24)-((to<<8)>>24)))):(((from<<8)>>24)+(D3DCOLOR)(factor*(float)(((to<<8)>>24)-((from<<8)>>24))))<<16)|((((from<<16)>>24)>((to<<16)>>24))?(((from<<16)>>24)-(D3DCOLOR)(factor*(float)(((from<<16)>>24)-((to<<16)>>24)))):(((from<<16)>>24)+(D3DCOLOR)(factor*(float)(((to<<16)>>24)-((from<<16)>>24))))<<8)|((((from<<24)>>24)>((to<<24)>>24))?(((from<<24)>>24)-(D3DCOLOR)(factor*(float)(((from<<24)>>24)-((to<<24)>>24)))):(((from<<24)>>24)+(D3DCOLOR)(factor*(float)(((to<<24)>>24)-((from<<24)>>24)))))));
}
D3DCOLOR is just a DWORD (unsigned long). A color can for example be 0xAARRGGBB (A-alpha, R-red, G-green, B-blue), but works with other compositions aswell.
Obviously it's a total mess, but this is exactly what I need.
The problem is that it doesn't work as intended:
GetFadedColor(0x00000000, 0xff33cccc, 0.3f)
// = 0x4c0f3d3d - working as intended
GetFadedColor(0xff33cccc, 0x00000000, 0.3f)
// = 0x000000bf - pretty wrong
GetFadedColor(0xff00ff00, 0x00ff00ff, 0.3f)
// = 0x004c00ff - second color value is correct, everything else wrong
I actually don't know how it works and don't remember where I have it from, so I'm asking here for help. Either help me find the error or find an alternative function that does exactly this.

What you should to now is first you should spend maybe 5 minutes to write down some really basic tests with the cases where you know what you expect. You don't even need to use any test framework, because to get rolling you could just use assert:
// basicTests.c
#include <assert.h>
int getFadedColor_basicTests()
{
assert(GetFadedColor(0x00000000, 0xff33cccc, 0.3f) == 0x4c0f3d3d && "30% from black to light blue should be greenish");
assert(GetFadedColor(0xff33cccc, 0x00000000, 0.3f) == something && "30% from one color to another should be...");
// if you're not sure what the exact value should be, you should write a helper function
// that returns true/false for if each of the four components of the actual color
// are in a sensible expected range
...
}
int main()
{
getFadedColor_basicTests();
return 0;
}
Once you're happy with how much coverage you get with tests, be it just 3 asserts total, or maybe 50 asserts if you feel like it, you should start reformatting the one-liner, breaking the line, adding meaningful indentation and comments. Start refactoring, extract out common expressions, add comments on what they do or should do, all while running the tests in between changes and adding tests as you devise new ones.
EDIT:
Isn't it just supposed to linearly extrapolate each of the components separately?
int fade(int from_, int to_, float factor)
{
unsigned char *from = (unsigned char*)&from_;
unsigned char *to = (unsigned char*)&to_;
int result_;
unsigned char *result = (unsigned char*)&result_;
for (int i = 0 ; i < 4; ++i)
{
result[i] = factor * ((int)to[i] - (int)from[i]) + from[i];
}
return result_;
}

Related

Are there any negative effects to using labels in C to actually label your code for improved readability?

I'm a self taught begginner in C and a few weeks ago I had the idea of using labels to label my code blocks, instead of comments. I'm not using them for flow control (I've never used goto) but to differentiate between my comments (I use them for bugs, todo, and "documentation") and my code block "headers" or "titles", to improve readability. For example:
int main()
{
while(true)
{
Update:
DoSomething1();
DoSomething2();
DoSomething3();
Render:
DrawSomething1();
DrawSomething2();
}
}
One thing to note is that if there's a declaration after the label the code won't compile, but you can add a ; after the label or use braces where appropiate (being mindful of scope etc)
Initialization:;
int width, height;
DoStuff:
{
width = 640;
height = 480;
}
I also use it for things like:
Player1: entities[0] = PlayerCreate(1);
Player2: entities[1] = PlayerCreate(2);
Are there any negative effects to doing this? Undefined behavior or something I should be aware of?
The top level comments are quite good at describing why using labels are a bad idea.
But, I'd like to add another reason:
They are not descriptive enough!
The labels are actually incomplete/bad comments!
Good comments should show intent. That is, the code shows the how. The comments explain the what and/or the why.
That is, if we do:
FILE *fin = fopen("data.txt","r");
What file are we opening? And, why are we opening it?
Here's a better attempt:
// open the customer file [so we can read it] and be able to validate the name
// and account number of the proposed transaction(s)
FILE *fin = fopen("data.txt","r");
In the first block, you have:
while (!MustClose())
Must close what and why?
Also, you have Update: and Render:
Again, what are you updating and why? What are you rendering and why?
In the second block in you have:
Initialization:;
int width, height;
You are not initializing here. You are defining. And, width and height are the dimensions of what? A drawing pad, a window, a surface, the main window, a subwindow?
You would be better off with (e.g.):
// dimensions of main window (in pixels)
int width, height;
In the third block you have:
Player1: entities[0] = PlayerCreate(1);
Player2: entities[1] = PlayerCreate(2);
Here Player1: is redundant extra cruft. It's redundant to PlayerCreate(1)
It's like doing:
x = 5; // set the value of x to 5
And, there is nothing to describe what the block is actually doing. We have to infer that we're creating players from PlayerCreate.
And, what if entities has (e.g.) 1,000 elements? Then, we'd do:
// create all players
for (int i = 0; i < 1000; ++i)
entities[i] = PlayerCreate(i + 1);

Issues using realloc (old size)

I'm trying to use realloc function in C, to dynamically operate on a char array of strings (char**).
I usually get a realloc():invalid old size error after 41st cicle of the for loop and I really can't understand why.
So, thanks to everyone who will help me ^-^
[EDIT] I'm trying to make the post more clear and following your advices, as a "new active member" of this community, so thank you all!
typedef struct _WordsOfInterest { // this is in an header file containing just
char **saved; // the struct and libraries
int index;
} wordsOfInterest;
int main() {
char *token1, *token2, *save1 = NULL, file[LEN], *temp, *word, **tokenArr;
int n=0, ch,ch2, flag=0, size, init=0,position,currEdit,init2=0,tempEdit,size_arr=LEN,
oldIndex=0,totalIndex=0,*editArr,counterTok=0;
wordsOfInterest toPrint;
char **final;
toPrint.index = 0;
toPrint.saved = malloc(sizeof(char*)*LEN);
editArr = malloc(sizeof(int)*LEN);
tokenArr = malloc(sizeof(char*)*LEN);
final = malloc(sizeof(char*)*1);
// external for loop
for(...) {
tokenArr[counterTok] = token1;
// internal while loop
while(...) {
// some code here surely not involved in the issue
} else {
if(init2 == 0) {
currEdit = config(token1,token2);
toPrint.saved[toPrint.index] = token2;
toPrint.index++;
init2 = 1;
} else {
if((abs((int)strlen(token1)-(int)strlen(token2)))<=currEdit) {
if((tempEdit = config(token1,token2)) == currEdit) {
toPrint.saved[toPrint.index] = token2;
toPrint.index++;
if(toPrint.index == size_arr-1) {
size_arr = size_arr*2;
toPrint.saved = realloc(toPrint.saved, size_arr);
}
} else if((tempEdit = config(token1,token2))<currEdit) {
freeArr(toPrint, size_arr);
toPrint.saved[toPrint.index] = token2;
toPrint.index++;
currEdit = tempEdit;
}
}
}
flag = 0;
word = NULL;
temp = NULL;
freeArr(toPrint, size_arr);
}
}
editArr[counterTok] = currEdit;
init2 = 0;
totalIndex = totalIndex + toPrint.index + 1;
final = realloc(final, (sizeof(char*)*totalIndex));
uniteArr(toPrint, final, oldIndex);
oldIndex = toPrint.index;
freeArr(toPrint,size_arr);
fseek(fp2,0,SEEK_SET);
counterTok++;
}
You start with final uninitialized.
char **final;
change it to:
char **final = NULL;
Even if you are starting with no allocation, it needs a valid value (e.g. NULL) because if you don't initialize a local variable to NULL, it gets garbage, and realloc() will think it is reallocating a valid chunk of memory and will fail into Undefined Behaviour. This is probably your problem, but as you have eliminated a lot of code in between the declaration and the first usage of realloc, whe cannot guess what is happening here.
Anyway, if you have indeed initialized it, I cannot say, as you have hidden part of the code, unlistening the recommendation of How to create a Minimal, Reproducible Example
.
There are several reasons (mostly explained there) to provide a full but m inimal, out of the box, failing code. This allows us to test that code without having to provide (and probably solving, all or part) the neccesary code to make it run. If you only post a concept, you cannot expect from us complete, full running, and tested code, degrading strongly the quality of SO answers.
This means you have work to do before posting, not just eliminating what you think is not worth mentioning.
You need to build a sample that, with minimum code, shows the actual behaviour you see (a nonworking complete program) This means eliminating everything that is not related to the problem.
You need (and this is by far more important) to, before sending the code, to test it at your site, and see that it behaves as you see at home. There are many examples that, when eliminated the unrelated code, don't show the commented behaviour.
...and then, without touching anymore the code, send it as is. Many times we see code that has been touched before sending, and the problem dissapeared.
If we need to build a program, we will probably do it with many other mistakes, but not yours, and this desvirtuates the purpose of this forum.
Finally, sorry for the flame.... but it is necessary to make people read the rules.

Using a c-program to read an NMEA-string

I am trying to make a c-program that will will a string, but I want it only to read a very small part of it.
The NMEA-telegram that I try to read is $WIXDR, and do receive the necessary strings.
Here's 2 examples of strings that I get into the CPU:
$WIXDR,C,1.9,C,0,H,83.2,P,0,P,1023.9,H,0*46
$WIXDR,V,0.01,M,0,Z,10,s,0,R,0.8,M,0,V,0.0,M,1,Z,0,s,1,R,0.0,M,1,R,89.9,M,2,R,0.0,M,3*60
If it were only 1 string (not both C and V), this would not be a problem for me.
The problem here is that it's 2 seperate strings. One with the temperature, and one with rain-info.
The only thing that I'm interested in is the value "1.9" from
$WIXDR,C,1.9,C,0......
Here's what I have so far:
void ProcessXDR(char* buffPtr)
{
char valueBuff[10];
int result, x;
float OutSideTemp;
USHORT uOutSideTemp;
// char charTemperature, charRain
IODBerr eCode;
//Outside Temperature
result = ReadAsciiVariable(buffPtr, &valueBuff[0], &buffPtr, sizeof(valueBuff));
sscanf(&valueBuff[0],"%f",&OutSideTemp);
OutSideTemp *= 10;
uOutSideTemp = (USHORT)OutSideTemp;
eCode = IODBWrite(ANALOG_IN,REG_COM_XDR,1,&uOutSideTemp,NULL);
}
// XDR ...
if(!strcmp(&nmeaHeader[0],"$WIXDR"))
{
if(PrintoutEnable)printf("XDR\n");
ProcessXDR(buffPtr);
Timer[TIMER_XDR] = 1200; // Update every minute
ComStateXDR = 1;
eCode = IODBWrite(DISCRETE_IN,REG_COM_STATE_XDR,1,&ComStateXDR,NULL);
}
There's more, but this is the main part that I have.
I have found the answer to my own question. The code that would do as I intented is as follows:
What my little code does, is to look for the letter C, and if the C is found, it will take the value after it and put it into "OutSideTemp". The reason I had to look for C is that there is also a similar string received with the letter V (Rain).
If someone have any input in a way it could be better, I don't mind, but this little piece here does what I need it to do.
Here's to example telegrams I receive (I wanted the value 3.0 to be put into "OutSideTemp"):
$WIXDR,C,3.0,C,0,H,59.2,P,0,P,1026.9,H,04F
$WIXDR,V,0.00,M,0,Z,0,s,0,R,0.0,M,0,V,0.0,M,1,Z,0,s,1,R,0.0,M,1,R,89.9,M,2,R,0.0,M,358
void ProcessXDR(char* buffPtr)
{
char valueBuff[10];
int result, x;
float OutSideTemp;
USHORT uOutSideTemp;
// char charTemperature, charRain
IODBerr eCode;
// Look for "C"
result = ReadAsciiVariable(buffPtr, &valueBuff[0], &buffPtr, sizeof(valueBuff));
// sscanf(&valueBuff[0],"%f",&charTemperature);
if (valueBuff[0] == 'C')
//Outside Temperature
result = ReadAsciiVariable(buffPtr, &valueBuff[0], &buffPtr, sizeof(valueBuff));
sscanf(&valueBuff[0],"%f",&OutSideTemp);
OutSideTemp *= 10;
uOutSideTemp = (USHORT)OutSideTemp;
eCode = IODBWrite(ANALOG_IN,REG_COM_XDR,1,&uOutSideTemp,NULL);
}

Two functions in C collide when used together but work just fine when used alone, what can i do?

I´m trying to make animation by using an address array with an algorithm to calculate the current picture of the animation.
I got two functions, one is a simple "1 to N counter", the other one includes direction, so i can change the direction somewhere and the function therefore starts to only play the animation for that direction.
The image addresses of all direction are saved in an array so that for example 1-3 are up, 4-6 are right, 7-9 are down and 10-12 are left.
Now to my problem:
If i use both my functions the program stops running.
If i only use one function it all works just fine, and since both functions are basically the same just with an other algorithm i don't understand why it stops when i run both functions.
I generate my address array with my own function so i don't need to have tons of rows with:
adr[1] = "bla_1";
adr[2] = "bla_2";
... but it doesn´t work as soon as i use 2 'animation functions'.
I tried to not use the function and write the addresses in by hand, now it works just fine. But that's no possible solution for me.
So i guess i made a mistake inside my address array generating function, even so it works just fine as long as i only use one 'animation function'.
______________________________MAIN_______________________________
char* playerAdr[8];
int count = 0;
char intString[8];
int gvPlayerDir = 2; // just for example!
loadAdrArrPaths( playerAdr, 8, "src/images/player" );
/*
playerAdr[0] = "src/images/player_1.png";
playerAdr[1] = "src/images/player_2.png";
...
playerAdr[7] = "src/images/player_8.png";
*/
printMultiAnimation( playerAdr, 2, 300, setPos(82,50), 1, gvPlayerDir );
//printAnimation( playerAdr, 8, 300, setPos(50,50), 1 );
______________________________HEAD_______________________________
void printAnimation( char* adrArr[], int adrNr, int frameTime, sPos pos, bool transparent ){
int tempInt = ((SDL_GetTicks() - gvTimestamp) / frameTime) % adrNr;
printSurface( adrArr[tempInt], pos, transparent );
}
void printMultiAnimation( char* adrArr[], int adrNr, int frameTime, sPos pos, bool transparent, int dir ){
int tempInt = (((SDL_GetTicks() - gvTimestamp) / frameTime) % adrNr) + ( adrNr * ( dir - 1 ));
printSurface( adrArr[tempInt], pos, transparent );
}
void loadAdrArrPaths( char* adrArr[], int size, char* path ){
int count = 0;
char intString[8];
char tempPath[size][256];
while( count < size ){
sprintf(intString, "%d", count+1);
strcpy( tempPath[count], path );
strcat( tempPath[count], "_" );
strcat( tempPath[count], intString );
strcat( tempPath[count], ".png" );
adrArr[count] = tempPath[count];
//printf("%i - %s\n", count, adrArr[count]);
count++;
}
}
All i want is to use printAnimation and printMultiAnimation together, so that i can have some 'simple animations' and some 'direction based animations' at the same time on my screen.
And like i said, if i declare the images addresses 'by hand' it works just fine so I'm pretty sure i do something wrong in my loadAdrArrPaths -function. Most likely I screwed up with pointers/references?
I hope someone can and will help me.
The problem is in loadAdrArrPaths, in the following line:
adrArr[count] = tempPath[count];
The array tempPath is local to loadAdrArrPaths, so its contents are no longer available after that function returns. The cases where this code works do so by luck. The slightest change can cause the stack to be overwritten, changing the contents of the array.
You can fix it by either having the caller pass the array to loadAdrArrPaths, or by dynamically allocating the storage with malloc.

How can I goto the value of a String in C?

I am programming a robot in C, and I have run into a problem I can't seem to figure out.
The only way to solve this problem would be to use a lot of goto statements.
I am trying to figure out a way to save myself writing over 100 goto points and statements and if statements, etc. and am wondering if there is a way to goto the value of a string. for example-
string Next = "beginning";
goto Next;
beginning:
Is there any way to goto the value of Next, or to substitute in the value of Next into the goto statement?
If there is a way to do this, then I will be able to just change the value of Next for each driving command, and then goto whatever the value of the string Next is.
In other words, just converting the string to a goto identifier, or substituting it in place of one.
Thanks for the help!
-EDIT-
A lot of you guys are suggesting the use of switch statements. I am not sure this would work because of how i have it programmed. The structure of the program is here--
by the way this code only includes a little of what i actually have, my real code is over 500 lines so far. Also, the driving commands are majorly simplified. but the basic concept is here, easier to understand than what i wouldve had.
task main()
{
//integer list
int forwardDrivingSelector = 0;
int backwardDrivingSelector = 0;
int rightRotatingSelector = 0;
string nextCommand;
int waitTime = 0;
int countup = 0;
//driving commands
driveForward:
while(forwardDrivingSelector == 1)
{
motor[leftMotor] = 127;
motor[rightMotor] = 127;
countup++;
wait1Msec(1);
if(countup == waitTime)
{
countup = 0;
goto nextCommand;
}
}
driveBackward:
while(backwardDrivingSelector == 1)
{
motor[leftMotor] = -127;
motor[rightMotor] = 127;
countup++;
wait1Msec(1);
if(countup == waitTime)
{
countup = 0;
goto nextCommand;
}
}
rightRotate:
while(rightRotatingSelector == 1)
{
motor[leftMotor] = 127;
motor[rightMotor] = -127;
countup++;
wait1Msec(1);
if(countup == waitTime)
{
countup = 0;
goto nextCommand;
}
}
//autonomous driving code
//first command, drive forward for 1000 milliseconds
forwardDrivingSelector = 1;
nextCommand = "secondCommand";
waitTime = 1000;
goto driveForward;
secondCommand:
forwardDrivingSelector = 0;
//second command, rotate right for 600 milliseconds
rightRotatingSelector = 1;
nextCommand = "thirdCommand";
waitTime = 600;
goto rightRotate;
thirdCommand:
rightRotatingSelector = 0;
//third command, drive backwards for 750 milliseconds
backwardDrivingSelector = 1;
nextCommand = "end";
waitTime = 750;
goto driveBackward;
end:
backwardDrivingSelector = 0;
}
so. how this works.
i have a list of integers, including driving command selectors, the countup and waitTime integers, and the string that i was talking about, nextCommand.
next comes the driving commands. in my real code, i have about 30 commands, and they are all hooked up to a remote control and its over 400 lines for just the driving commands.
next comes the autonomous code. the reason i set it up like this is so that the autonomous code part would be, short, simple, and to the point. pretty much to add a command to the driving code, you turn on the selector, tell the nextCommand string what the next command is, set the waitTime (which is how long it does the command, in milliseconds), then you make the code goto the driving command which you are putting in. the driving command drives for the amount of time you put in, then does goto nextCommand;
This would all theoretically work if there was a way to make the goto statement 'interpret' the string as an identifier so it can be changed.
There are about 4 simple ways i can think of right now that could get past this easily, but they would make the code really really long and cluttered.
Now that you have a better understanding of my question, any more input? :)
btw - i am using a program called robotC, and i am programming a vex robot. so i HAVE to use plain, basic, C, and i cant use any addons or anything... which is another reason this is complicated because i cant have multiple classes and stuff like that...
As an extension to the C language, GCC provides a feature called computed gotos, which allow you to goto a label computed at runtime. However, I strongly recommend you reconsider your design.
Instead of using gotos with over a hundred labels (which will easily lead to unmaintainable spaghetti code), consider instead using function pointers. The code will be much more structured and maintainable.
Instead of goto's, I'd call one of 100 functions. While C won't handle the conversion from string to function for you, it's pretty easy to use a sorted array of structs:
struct fn {
char name[whatever];
void (*func)(void);
};
Then do (for example) a binary search through the array to find the function that matches a string.
Also note that many real systems provide things like GetProcAddress (Windows) or dlsym (Unix/Linux) to handle some of the work for you.
You're thinking about this the wrong way. Each of the actions you need to call should be a function, then you can choose which function should be called next by inspecting a "next" variable.
This could be a string as you've mentioned, but you might be best using a enumerated type to make readable, but more efficient code.
The alternative, though probably overkill, would be to ensure your functions all use the same parameters and return types, and then use a function pointer to track which piece of code should be executed next.
Small tip: If you ever think you need more than 1 goto statement to achieve a certain goal you're probably not looking at the best solution.
You need to step back and consider other solutions for the problem you are trying to solve. One of them might look like this:
void DoSomething() {
printf("Something\n");
}
void DoSomethingElse() {
printf("Something else\n");
}
void (*nextStep)(void) = NULL;
nextStep = DoSomething;
nextStep();
nextStep = DoSomethingElse;
nextStep();
See it in action.
How about a switch? Either use an int/enum/whatever or inspect the value of the string (loop over it and strcmp, for instance) to figure out the destination.
const char *dsts[n_dsts] = {"beginning","middle",...};
...
int i;
for(i = 0; i < n_dsts; i++) if(strcmp(dsts[i]) == 0) break;
switch(i) {
case 0: // whatever
case 1: // whatever
...
break;
default: // Error, dest not found
}
Firstly, let me preface this by agreeing with everyone else: this is probably not the right way to go about what you're trying to do. In particular, I think you probably want a finite-state machine, and I recommend this article for guidelines on how to do that.
That said . . . you can more or less do this by using a switch statement. For example:
Next = BEGINNING;
HelperLabel:
switch(Next)
{
case BEGINNING:
.
.
.
Next = CONTINUING;
goto HelperLabel;
case ENDING:
.
.
.
break;
case CONTINUING:
.
.
.
Next = ENDING;
goto HelperLabel;
}
(Note that a switch statement requires integers or integer-like values rather than strings, but you can use an enum to create those integers in a straightforward way.)
See http://en.wikipedia.org/wiki/Duff's_device for the original, canonical example of using switch/case as a goto.
#define GOTO_HELPER(str, label) \
if (strcmp(str, #label) == 0) goto label;
#define GOTO(str) do { \
GOTO_HELPER(str, beginning) \
GOTO_HELPER(str, end) \
} while (0)
int main (int argc, char ** argv) {
GOTO("end");
beginning:
return 1;
end:
return 0;
}

Resources