ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Are RGB values reversed in VS2022 Windows 10?
Posted by Brad Kimble on Sep-28-2024 08:04
I am evaluating ChartDirector as a possible replacement for an older OCX we were using in a Windows C++ program. We do mostly simple multi-line X/Y graphs and 1 bar chart. I have 2 initial questions after trying to implement the bar chart to start with.
1. When I pass in colors I use RGB(Red, Green, Blue) values, each with a range of 0-255. Pretty standard. However, in my bar chart, whenever I use a color, the Red and Blue seem to be reversed. As a simple test I displayed 1 bar with Red RGB(255, 0, 0) and one with Blue RGB(0,0, 255). I got the opposite colors I was expecting. I then reversed the values and called my Red = RGB(0, 0, 255) and Blue = RGB(255, 0, 0) and got the colors I was looking for. Green seems to be OK.  A sample of my code:
    BarLayer* pLayerStake1 = c->addBarLayer(Chart::Side);
    pLayerStake1->addDataSet(DoubleArray(dataStake1Long, dataStake1Long_size), RGB(0, 255, 0), "#1 Long"); // Green displays green filled bar.
    pLayerStake1->addDataSet(DoubleArray(dataStake1Short, dataStake1Short_size), RGB(255, 0, 0), "#1 Short");  // Should be Red, but gives a Blue filled bar.
        // Using RGB(0, 0, 255) gives a Red filled bar.

Am I missing something?

2. Unrelated to colors, I can't find how to set the Y axis to a fixed range. I have the axis:
    Axis* pYAxis = c->yAxis();
When I set my data values from 0 - 100, the y axis goes to 120. I want to have the max Y values on the axis be 100. (I know my values will not go above 100, it is actually a percent converted to a number 0-100)

Any hints would be appreciated.

  Re: Are RGB values reversed in VS2022 Windows 10?
Posted by Peter Kwan on Sep-28-2024 11:56
Hi Brad,

ChartDirector for C++ can work on Windows as well as Linux and it also works in other programming languages. For maximum compatibility, it uses the RGB as represented by the programming language (which is C++ in your case). For example, 0xFF0000 is red, and 0x0000FF is blue.

The RGB macro is specific to certain Microsoft frameworks, such as Windows GDI. For historical reasons, the RGB macro will reverse the red and blue components when displayed in C++ syntax. The followings are some examples published in Microsoft's web site (https://learn.microsoft.com/en-us/windows/win32/gdi/colorref)

const COLORREF rgbRed   =  0x000000FF;
const COLORREF rgbGreen =  0x0000FF00;
const COLORREF rgbBlue  =  0x00FF0000;

You can see the red color, equivalent to RGB(0xFF, 0, 0) or RGB(255, 0, 0), is 0000FF in C++. That is, the RGB macro puts the red component is the component on the left side when represented in C/C++ number format. You can consider the RGB is actually BGR in C/C++.

For ChartDirector, we suggest you use the hex number directly, like 0xff0000 for red. The hex format has the advantage that it supports semi-transparent colors, while the RGB has no transparency support.

If you think it is easier to use decimals as RGB components, you can consider to define your own macro, like:

#define ChartColor(R, G, B)  RGB(B, G, R)

Best Regards
Peter Kwan

  Re: Are RGB values reversed in VS2022 Windows 10?
Posted by Brad Kimble on Sep-29-2024 05:46
1. Thank you for the RGB explanation. I have been working in VS C/C++ a very long time and never realized that Microsoft's RGB reversed the colors for 'native' C/C++. I guess it's because since doing color graphics I have always worked on Windows environments. I will make the adjustment for code using ChartDirector.

2. I will re-submit my 2nd unrelated question separately.