翻譯色彩

轉譯會將值新增至四個色彩元件的一或多個。 下表提供代表翻譯的色彩矩陣專案。

要翻譯的元件 矩陣專案
紅色 [4][0]
綠色 [4][1]
藍色 [4][2]
Alpha [4][3]

 

下列範例會從檔案ColorBars.bmp建構 Image 物件。 然後程式碼會將 0.75 新增至影像中每個圖元的紅色元件。 原始影像會與轉換的影像一起繪製。

Image            image(L"ColorBars.bmp");
ImageAttributes  imageAttributes;
UINT             width = image.GetWidth();
UINT             height = image.GetHeight();

ColorMatrix colorMatrix = {
   1.0f,  0.0f, 0.0f, 0.0f, 0.0f,
   0.0f,  1.0f, 0.0f, 0.0f, 0.0f,
   0.0f,  0.0f, 1.0f, 0.0f, 0.0f,
   0.0f,  0.0f, 0.0f, 1.0f, 0.0f,
   0.75f, 0.0f, 0.0f, 0.0f, 1.0f};
   
imageAttributes.SetColorMatrix(
   &colorMatrix, 
   ColorMatrixFlagsDefault,
   ColorAdjustTypeBitmap);
   
graphics.DrawImage(&image, 10, 10, width, height);

graphics.DrawImage(
   &image, 
   Rect(150, 10, width, height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle 
   width,       // width of source rectangle
   height,      // height of source rectangle
   UnitPixel,
   &imageAttributes);

下圖顯示左側的原始影像和右邊的已轉換影像。

顯示四個彩色橫條的圖例,然後顯示不同色彩的相同橫條

下表列出紅色轉譯前後四個橫條的色彩向量。 請注意,因為色彩元件的最大值是 1,所以第二個數據列中的紅色元件不會變更。 (同樣地,色彩元件的最小值為 0.)

原始 已轉譯
黑色 (0、0、0、1) (0.75, 0, 0, 1)
紅色 (1、0、0、1) (1, 0, 0, 1)
綠色 (0、1、0、1) (0.75, 1, 0, 1)
藍色 (0, 0, 1, 1) (0.75, 0, 1, 1)