Reading colours from a STEP file with GetColour returns incorrect RGB values

Reading colours from a STEP file with GetColour returns incorrect RGB values.

I use the GetColour method to read the RGB values of surfaces. However, these are not determined correctly. Here is the code and the step. Do I still have to convert the values returned by open Cascade?

The following colours were used in the step:
pink (255, 0, 204)
orange (255, 153, 0)

the output is:

Color: MAGENTA2
Red: 1, Green: 0, Blue: 0.603827
RGB: (255, 0, 153)

Color: ORANGE
Red: 1, Green: 0.318547, Blue: 0
RGB: (255, 81, 0)

Code snippet:


     Quantity_Color color;

 if (ColorTool->GetColor(Shape, XCAFDoc_ColorGen, color) ||
     ColorTool->GetColor(Shape, XCAFDoc_ColorSurf, color) ||
     ColorTool->GetColor(Shape, XCAFDoc_ColorCurv, color))
 {
     std::cout << "Colorname: " << Quantity_Color::StringName(color.Name()) << std::endl;
     //std::cout << "HEX colour (OC * 255 ): " << std::hex << static_cast<int>(color.Red() * 255) + (static_cast<int>(color.Green() * 255) << 8) + (static_cast<int>(color.Blue() * 255) << 16) << std::endl;


     double redVal = color.Red();
     double greenVal = color.Green();
     double blueVal = color.Blue();
     std::cout << "Rohdaten von Open Cascade Red: " << redVal << ", Green: " << greenVal << ", Blue: " << blueVal << std::endl;

     int red = static_cast<int>(redVal * 255);
     int green = static_cast<int>(greenVal * 255);
     int blue = static_cast<int>(blueVal * 255);

     std::cout << "Open Cascade * 255 = RGB: (" << red << ", " << green << ", " << blue << ")" << std::endl;

     // Farbe berechnen
     int colour = static_cast<int>(color.Red() * 255) +
         (static_cast<int>(color.Green() * 255) << 8) +
         (static_cast<int>(color.Blue() * 255) << 16);



     // Fläche berechnen
     GProp_GProps properties;
     BRepGProp::SurfaceProperties(Shape, properties);
     double area = properties.Mass();
     

     if (results.count < results.capacity || results.capacity == 0) {
         SingleResult newResult = { area, colour, type };
         results.AddResult(newResult);
     }
     else {
         std::cerr << "Fehler: Ergebnis konnte nicht hinzugefügt werden!" << std::endl;
     }


     std::cout << " und Area:: " << area << std::endl;
     std::cout << "colour: " << colour << std::endl;
 }

 std::cout << std::endl;

Debug Console:

Colorname: ORANGE
Rohdaten von Open Cascade Red: 1, Green: 0.318547, Blue: 0
Open Cascade * 255 = RGB: (255, 81, 0)
 und Area:: 2500
colour: 20991

        FACE
Colorname: MAGENTA2
Rohdaten von Open Cascade Red: 1, Green: 0, Blue: 0.603827
Open Cascade * 255 = RGB: (255, 0, 153)
Area:: 2500
colour: 10027263
Attachments: 
gkv311 n's picture

You need converting linear RGB color to sRGB if you want to show values in 0..255 range:

     double redVal = 0.0, greenVal = 0.0, blueVal = 0.0;
     color.Values(redVal, greenVal, blueVal, Quantity_TOC_sRGB);

     int red   = static_cast<int>(Round(redVal * 255));
     int green = static_cast<int>(Round(greenVal * 255));
     int blue  = static_cast<int>(Round(blueVal * 255));
     std::cout << "Open Cascade * 255 = RGB: (" << red << ", " << green << ", " << blue << ")" << std::endl;

As for printing color in hex format - you may use Quantity_Color::ColorToHex():

std::cout << "HEX colour " << Quantity_Color::ColorToHex(color) << "\n";
Steffen König's picture

Thank you, that's the solution. The colours are now displayed correctly.

Thank you very much

[code]

// Farbwerte aus OpenCASCADE im sRGB-Farbraum abrufen
color.Values(redVal, greenVal, blueVal, Quantity_TOC_sRGB);

// Umrechnung in den Bereich [0, 255] und Rundung
int red = static_cast<int>(Round(redVal * 255));
int green = static_cast<int>(Round(greenVal * 255));
int blue = static_cast<int>(Round(blueVal * 255));

// Ausgabe der berechneten Farbwerte
std::cout << "RGB (sRGB-Farbraum): (" << red << ", " << green << ", " << blue << ")" << std::endl;
std::cout << "HEX colour " << Quantity_Color::ColorToHex(color) << "\n";

// Farbe übergeben
int colour = (red << 16) | (green << 8) | blue;

[/code]