Qt GUI: QPixelFormat::ColorModel をマスターする


QPixelFormat::ColorModel は、Qt GUI モジュールでピクセルフォーマットの色モデルを定義する列挙型です。ピクセルフォーマットは、画像やグラフィックデータの構成方法を記述する構造体であり、ColorModel はその重要な要素の一つです。

ColorModel の種類

現在、QPixelFormat::ColorModel には以下の種類があります。

  • Alpha
    アルファチャネルのみを含む特殊な色モデルです。透明度情報を表現するために使用されます。
  • YUV
    輝度 (Y)、色差 (U)、色差 (V) の 3つの値で色を表現します。
  • HSV
    HSL と似ていますが、値の範囲が異なります。
  • HSL
    色相 (Hue)、彩度 (Saturation)、輝度 (Lightness) の 3つの値で色を表現します。
  • CMYK
    印刷業界でよく使用される色モデルで、シアン (Cyan)、マゼンタ (Magenta)、イエロー (Yellow)、キー (Key) の 4つのチャネルで構成されます。
  • Grayscale
    明暗のみを表現する白黒画像で使用されます。
  • Indexed
    各ピクセルがインデックス値として表現され、そのインデックス値に基づいてパレットから色情報が取得されます。
  • BGR
    RGB と論理的に逆のエンディアンです。つまり、青、緑、赤の順序でチャネルが並んでいます。
  • RGB
    最も一般的な色モデルで、赤 (Red)、緑 (Green)、青 (Blue) の 3つのチャネルで構成されます。

ColorModel の選択

適切な ColorModel は、アプリケーションのニーズによって異なります。例えば、写真や画像の表示には RGB または BGR が一般的です。一方、印刷物を作成するには CMYK が必要です。

ColorModel の使用方法

QPixelFormat::ColorModel は、QPixelFormat 構造体のコンストラクタまたは setter メソッドを使用して設定できます。例えば、以下のコードは RGB 色モデルを持つピクセルフォーマットを作成します。

QPixelFormat format;
format.setColorModel(QPixelFormat::RGB);
  • QPixelFormat::ColorModel は、ピクセルフォーマットの色空間も定義します。色空間は、色を表現するために使用される数学的なモデルです。例えば、sRGB や Adobe RGB などの色空間があります。

以下のコードは、RGB 色モデルと 8 ビット/チャネルを持つピクセルフォーマットを作成し、その情報を表示します。

QPixelFormat format;
format.setColorModel(QPixelFormat::RGB);
format.setDepth(24);

qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

このコードを実行すると、以下の出力が得られます。

Color model: RGB
Depth: 24
Bits per channel: 8
Number of channels: 3


QPixelFormat format;
format.setColorModel(QPixelFormat::RGB);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

例 2: Indexed 色モデルを使用したピクセルフォーマットの作成

QPixelFormat format;
format.setColorModel(QPixelFormat::Indexed);

// パレットを作成
QImage paletteImage(16, 16);
for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 16; ++j) {
        paletteImage.setPixel(i, j, QColor(i * 16, j * 16, 0));
    }
}

// ピクセルフォーマットにパレットを設定
format.setPalette(paletteImage.palette());

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

例 3: Grayscale 色モデルを使用したピクセルフォーマットの作成

QPixelFormat format;
format.setColorModel(QPixelFormat::Grayscale);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

例 4: CMYK 色モデルを使用したピクセルフォーマットの作成

QPixelFormat format;
format.setColorModel(QPixelFormat::CMYK);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

例 5: HSL 色モデルを使用したピクセルフォーマットの作成

QPixelFormat format;
format.setColorModel(QPixelFormat::HSL);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

例 6: HSV 色モデルを使用したピクセルフォーマットの作成

QPixelFormat format;
format.setColorModel(QPixelFormat::HSV);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();

例 7: YUV 色モデルを使用したピクセルフォーマットの作成

QPixelFormat format;
format.setColorModel(QPixelFormat::YUV);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();
QPixelFormat format;
format.setColorModel(QPixelFormat::Alpha);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();


QPixelFormat format;
format.setColorModel(QPixelFormat::Indexed);

// パレットを作成
QImage paletteImage(16, 16);
for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 16; ++j) {
        paletteImage.setPixel(i, j, QColor(i * 16, j * 16, 0));
    }
}

// ピクセルフォーマットにパレットを設定
format.setPalette(paletteImage.palette());

アルファチャネルの使用

QPixelFormat には、setAlphaUsage() メソッドと setAlphaInfo() メソッドを使用してアルファチャネルの使用方法を設定することができます。

  • setAlphaInfo() メソッドは、アルファチャネルのフォーマットを指定します。QPixelFormat::Premultiplied または QPixelFormat::NotPremultiplied のいずれかを指定できます。
  • setAlphaUsage() メソッドは、アルファチャネルがどのように使用されるかを指定します。QPixelFormat::IgnoresAlpha または QPixelFormat::UsesAlpha のいずれかを指定できます。

以下のコードは、パレットを使用してカラーマップを作成し、アルファチャネルを使用するピクセルフォーマットを作成します。

QPixelFormat format;
format.setColorModel(QPixelFormat::Indexed);

// パレットを作成
QImage paletteImage(16, 16);
for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 16; ++j) {
        paletteImage.setPixel(i, j, QColor(i * 16, j * 16, 0));
    }
}

// ピクセルフォーマットにパレットを設定
format.setPalette(paletteImage.palette());

// アルファチャネルの使用を設定
format.setAlphaUsage(QPixelFormat::UsesAlpha);
format.setAlphaInfo(QPixelFormat::Premultiplied);

// ピクセルフォーマット情報を表示
qDebug() << "Color model:" << format.colorModel();
qDebug() << "Depth:" << format.depth();
qDebug() << "Bits per channel:" << format.bitsPerChannel();
qDebug() << "Number of channels:" << format.channelCount();
qDebug() << "Alpha usage:" << format.alphaUsage();
qDebug() << "Alpha info:" << format.alphaInfo();