Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 褪色函数中的误差_C_Colors_Code Snippets - Fatal编程技术网

C 褪色函数中的误差

C 褪色函数中的误差,c,colors,code-snippets,C,Colors,Code Snippets,我在snippets文件夹中找到了这个旧的颜色褪色函数,并希望将其实现到我的一个项目中。它可以用来将一种颜色淡入另一种颜色。这是一条很长的单行线: D3DCOLOR GetFadedColor(D3DCOLOR from, D3DCOLOR to, float factor) { return (factor<0.0f)?from:((factor>1.0f)?to:((((from>>24)>(to>>24))?((from>>24

我在snippets文件夹中找到了这个旧的颜色褪色函数,并希望将其实现到我的一个项目中。它可以用来将一种颜色淡入另一种颜色。这是一条很长的单行线:

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)))))));
}

我不知道它是怎么工作的,也不记得它是从哪里来的,所以我在这里寻求帮助。要么帮我找到错误,要么找一个替代函数来实现这一点。

你现在应该做的是,首先,你应该花5分钟的时间写下一些真正基本的测试,在这些情况下,你知道你期望的是什么。您甚至不需要使用任何测试框架,因为要开始运行,您可以使用
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;
}
//basicTests.c
#包括
int getFadedColor_基本测试()
{
断言(GetFadedColor(0x00000000,0xff33cccc,0.3f)=0x4c0f3d3d&“从黑色到浅蓝色的30%应为绿色”);
断言(GetFadedColor(0xff33cccc,0x00000000,0.3f)=某物&&“从一种颜色到另一种颜色的30%应该是…”);
//如果您不确定确切的值应该是多少,那么应该编写一个helper函数
//对于实际颜色的四个分量中的每一个,返回true/false
//在合理的预期范围内
...
}
int main()
{
getFadedColor_基本测试();
返回0;
}
一旦您对测试的覆盖率感到满意,无论是总共3个断言,或者如果您愿意,也可以是50个断言,您应该开始重新设置一行代码的格式,打破界限,添加有意义的缩进和注释。开始重构、提取常见表达式、添加关于它们所做或应该做的事情的注释,同时在更改之间运行测试,并在设计新的测试时添加测试

编辑:

它不是应该分别对每个组件进行线性外推吗

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_;
}
int淡入度(int-from,int-to,float-factor)
{   
无符号字符*from=(无符号字符*)&from;
无符号字符*到=(无符号字符*)&to;
int-result_u2;;
无符号字符*结果=(无符号字符*)&result;
对于(int i=0;i<4;++i)
{
结果[i]=系数*((int)至[i]-(int)自[i])+自[i];
}   
返回结果;
}

谢谢。我希望有人能发现错误,或者有一个更干净的解决方案。这段代码似乎很冗余,似乎有点太多了。@typ1232我的解决方案有帮助吗?非常感谢!这正是应该发生的事情。而且它是人类可读的!
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_;
}