更改DWORD颜色alpha通道值

时间:2014-07-06 03:45:44

标签: c++ winapi dword

我有一个起始颜色:0xffff00ff,它是:255,r:255,g:0,b:255。

目标是根据百分比将颜色的Alpha通道更改为不透明。即该颜色的50%不透明度大约为0x80ff00ff。

我是如何尝试达成解决方案的:

DWORD cx = 0xffff00ff;
DWORD cn = .5;

DWORD nc = cx*cn;

5 个答案:

答案 0 :(得分:2)

DWORD cx = 0xffff00ff;
float cn = .5;
DWORD alphaMask=0xff000000;
DWORD nc = (cx|alphaMask)&((DWORD)(alphaMask*cn)|(~alphaMask));

这应该可以解决问题。我在这里所做的就是将DWORD的前8位设置为1并用(用'|'表示),然后用你想要的正确值将这些位设置为alpha掩码乘以cn。当然,我将乘法的结果再次转换为DWORD。

答案 1 :(得分:1)

typedef union ARGB
{
    std::uint32_t Colour;
    std::uint8_t A, R, G, B;
};


int main()
{
    DWORD cx = 0xffff00ff;
    reinterpret_cast<ARGB*>(&cx)->A = reinterpret_cast<ARGB*>(&cx)->A / 2;

    std::cout<<std::hex<<cx;
}

答案 2 :(得分:1)

这是经过测试的代码(在linux中)。但是,您可能会找到一个更简单的答案。注意:这是RGBA,而不是您在问题中引用的ARGB。

double transparency = 0.500;

unsigned char *current_image_data_iterator = reinterpret_cast<unsigned char*>( const_cast<char *>( this->data.getCString() ) );
unsigned char *new_image_data_iterator = reinterpret_cast<unsigned char*>( const_cast<char *>( new_image_data->data.getCString() ) );

size_t x;

//cout << "transparency: " << transparency << endl;

for( x = 0; x < data_length; x += 4 ){

    //rgb data is the same
    *(new_image_data_iterator + x) = *(current_image_data_iterator + x);
    *(new_image_data_iterator + x + 1) = *(current_image_data_iterator + x + 1);
    *(new_image_data_iterator + x + 2) = *(current_image_data_iterator + x + 2);

    //multiply the current opacity by the applied transparency
    *(new_image_data_iterator + x + 3) = uint8_t( double(*(current_image_data_iterator + x + 3)) * ( transparency / 255.0 ) );

    //cout << "Current Alpha: " << dec << static_cast<int>( *(current_image_data_iterator + x + 3) ) << endl;
    //cout << "New Alpha: " << double(*(current_image_data_iterator + x + 3)) * ( transparency / 255.0 ) << endl;
    //cout << "----" << endl;

}

答案 3 :(得分:1)

我选择的解决方案:

DWORD changeOpacity(DWORD color, float opacity) {
    int alpha = (color >> 24) & 0xff;
    int r = (color >> 16) & 0xff;
    int g = (color >> 8) & 0xff;
    int b = color & 0xff;

    int newAlpha = ceil(alpha * opacity);

    UINT newColor = r << 16;
    newColor += g << 8;
    newColor += b;
    newColor += (newAlpha << 24);


    return (DWORD)newColor;
}

答案 4 :(得分:0)

我理解你的问题:我希望在保持相同的整体透明度的同时,将某个给定的rgba颜色分量改变一定的因素。

对于具有完整alpha(1.0或255)的颜色,这是微不足道的:只需将组件相乘而不触及其他组件:

//typedef unsigned char uint8
enum COMPONENT {
    RED,
    GREEN,
    BLUE,
    ALPHA
};

struct rgba {
    uint8 components[4];
    // uint8 alpha, blue, green, red; // little endian
    uint8 &operator[](int index){
        return components[index];
    }
};

rgba color;
if (color[ALPHA] == 255)
   color[RED] *= factor;
else
    ComponentFactor(color, RED, factor);
在一般情况下,这个问题可能没有一个答案。考虑到颜色可以在HSL or HSV中交替编码。您可能希望保留其中一些参数,并允许其他参数更改。

我解决这个问题的方法是首先尝试在完全alpha下找到源颜色和目标颜色之间的色调距离,然后将真实源颜色转换为HSV,应用色调变化,然后转换回RGBA。显然,如果alpha实际上是1.0,则不需要第二步。

在伪代码中:

rgba ComponentFactor(rgba color, int component, double factor){
    rgba fsrc = color, ftgt;
    fsrc.alpha = 1.0; // set full alpha
    ftgt = fsrc;
    ftgt[component] *= factor; // apply factor
    hsv hsrc = fsrc, htgt = ftgt; // convert to hsv color space
    int distance = htgt.hue - hsrc.hue; // find the hue difference
    hsv tmp = color; // convert actual color to hsv
    tmp.hue += distance; // apply change in hue
    rgba res = tmp; // convert back to RGBA space 
    return res;
}

注意上面的内容依赖于类型rgbahsv来拥有隐式转换构造函数。可以通过网络搜索轻松找到用于转换的算法。从rgba派生hsv的结构定义也应该很容易,或者将单个组件访问作为字段成员包括(而不是使用[]运算符)。 例如:

//typedef DWORD uint32;

struct rgba {

    union {
        uint8 components[4];
        struct {
            uint8 alpha,blue,green,red; // little endian plaform
        }
        uint32 raw;
    };

    uint8 &operator[](int index){
        return components[4 - index];
    }
    rgba (uint32 raw_):raw(raw_){}
    rgba (uint8 r, uint8 g, uint8 b, uint8 a):
        red(r), green(g), blue(b),alpha(a){}
};

也许您必须找到一个色调因子而不是距离,或者调整其他HSV组件以达到预期效果。