Bug in ColorToString

Angelo Ferraro
282
Angelo Ferraro  

Hello everybody,

I have a problem with the ColorToString () function.

The second parameter is ignored. I set it to false but the function still returns the color name.

   str_clr = ColorToString(colorDw,false);
   str_size = StringLen(str_clr);

This the result in debug

Vladimir Karputov
Moderator
250612
Vladimir Karputov  

There is no mistake. Reference: ColorToString

Parameters

color_value

[in]  Color value in color type variable.

color_name

[in]  Return color name if it is identical to one of predefined color constants.

Return Value

String presentation of color as "R,G,B", where R, G and B are decimal constants from 0 to 255 converted into a string. If the color_name=true parameter is set, it will try to convert color value into color name.


Therefore, the function ALWAYS returns the NAME of the color. But if true, an attempt is made to convert the color value to a color name.

Documentation on MQL5: Conversion Functions / ColorToString
Documentation on MQL5: Conversion Functions / ColorToString
  • www.mql5.com
ColorToString - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Angelo Ferraro
282
Angelo Ferraro  
Vladimir Karputov:

There is no mistake. Reference: ColorToString

Parameters

color_value

[in]  Color value in color type variable.

color_name

[in]  Return color name if it is identical to one of predefined color constants.

Return Value

String presentation of color as "R,G,B", where R, G and B are decimal constants from 0 to 255 converted into a string. If the color_name=true parameter is set, it will try to convert color value into color name.


Therefore, the function ALWAYS returns the NAME of the color. But if true, an attempt is made to convert the color value to a color name.

ALWAYS?????
No, only if the color_name=true parameter is set.

In the code I set it to false.

Vladimir Karputov
Moderator
250612
Vladimir Karputov  

You are confusing two concepts: color name and color constant.

To be honest, I generally think that the bool parameter is NOT NECESSARY.

Angelo Ferraro
282
Angelo Ferraro  
Vladimir Karputov:

You are confusing two concepts: color name and color constant.

To be honest, I generally think that the bool parameter is NOT NECESSARY.

I agree.

Thanks anyway

Vladimir Karputov
Moderator
250612
Vladimir Karputov  
Angelo Ferraro :

I agree.

Thanks anyway

Isn't it hard for you to tell which cases you use the ColorToString () function?

Angelo Ferraro
282
Angelo Ferraro  

No problem.

I use it to save settings on file (I convert colors to strings first).

   string str_clr = ColorToString(colorUp,false);
   str_size = StringLen(str_clr);
   FileWriteInteger(file_handle, str_size, INT_VALUE); 
   FileWriteString(file_handle, str_clr, -1); 

Opposite conversion when reading back from files

Alain Verleyen
41734
Alain Verleyen  
Vladimir Karputov:

There is no mistake. Reference: ColorToString

Parameters

color_value

[in]  Color value in color type variable.

color_name

[in]  Return color name if it is identical to one of predefined color constants.

Return Value

String presentation of color as "R,G,B", where R, G and B are decimal constants from 0 to 255 converted into a string. If the color_name=true parameter is set, it will try to convert color value into color name.


Therefore, the function ALWAYS returns the NAME of the color. But if true, an attempt is made to convert the color value to a color name.

You are wrong, according to the documentation it's clearly a bug.

   Print(ColorToString(C'0,255,0',false));    // Should return "R,G,B", so "0,255,0"

It returns :

2021.04.30 01:26:43.528    te (EURUSD,M5)    clrLime

Vladimir Karputov
Moderator
250612
Vladimir Karputov  

I think that the function works correctly without the participation of the 'bool' parameter - the function tries to return the name of the color, if there is no such color (such a color constant), then the output will be in the form of RGB.

Example:

   string clr=ColorToString(C'0,254,0'); // color
   Print(clr);
   clr=ColorToString(C'0,255,0'); // color
   Print(clr);

Result:

0,254,0
clrLime
Vladimir Karputov
Moderator
250612
Vladimir Karputov  
Asked a question in a profile topic: 
Новая версия платформы MetaTrader 5 build 2875: Улучшения и исправления
Новая версия платформы MetaTrader 5 build 2875: Улучшения и исправления
  • 2021.04.30
  • www.mql5.com
В пятницу 2 апреля 2021 года будет выпущена обновленная версия платформы MetaTrader 5...
Angelo Ferraro
282
Angelo Ferraro  
Alain Verleyen:

You are wrong, according to the documentation it's clearly a bug.

It returns :

2021.04.30 01:26:43.528    te (EURUSD,M5)    clrLime

What I thought too (beyond the usefulness of the second parameter).

thank you both