The function of decomposing color into shades. - page 14

 
Yury Kulikov:


Check your algorithm for colour 0,0,0 and 255,255,255.

I will check it now.

 
Yury Kulikov:


Check your algorithm for colour 0,0,0 and 255,255,255.

Checked. 0,0,0,0, - causes an error. Advisor's unloading.

255,255,255 is fine.

Thank you for this observation. I'll look into it.

 
Реter Konow:

What do you think, Nikolai?

Would you admit that you've made a lot of noise out of nothing? Yes, the algorithm may be slower than yours and it had a couple of bugs. It's not debugged on MT5 yet. But it works.

Do you even realize that code like this

//-----------------------------------------------
   if(Старшая_компонента == Исходный_R)R = Старшая_компонента;
   if(Старшая_компонента == Исходный_G)G = Старшая_компонента;
   if(Старшая_компонента == Исходный_B)B = Старшая_компонента;
//------------------------
   if(Средняя_компонента == Исходный_R)R = Средняя_компонента;
   if(Средняя_компонента == Исходный_G)G = Средняя_компонента;
   if(Средняя_компонента == Исходный_B)B = Средняя_компонента;
//------------------------
   if(Младшая_компонента == Исходный_R)R = Младшая_компонента;
   if(Младшая_компонента == Исходный_G)G = Младшая_компонента;
   if(Младшая_компонента == Исходный_B)B = Младшая_компонента;

is 100% the same as this one:

R = Исходный_R;
G = Исходный_G;
B = Исходный_B;

After that, you shouldn't wonder why your code is so much slower.

 
Реter Konow:

255,255,255 is fine.

Is this consistent with 99% "windows shades"?

 
Yury Kulikov:

I'll join the 'accusations', couldn't take it :)

A good example of how not to program. If this is how your entire GUI is written, we won't be seeing it for a while. :(

Every line is a "masterpiece": such a pile of errors and bloopers, so much hope that mql4 will work out. I now understand why mt4 is being used.

I think that publishing of such codes and the subsequent reaction to criticism is not respecting the forum audience. They don't want to do you harm, they want to help you.

As for comparing algorithms, you can compare them visually. It's not like you offered proof in numbers that your algorithm gives shades 99% close to "windows shades".

On the left is sort of your approach, on the right isNikolai Semko's. (A modified NikolaiSemko script was used)


Thank you, Yuri, for your participation and understanding of the matter, as well as for the clear example. After your changes my code became more pleasant to look at.

But here is a small clarification:

int x;//индекс исходного цвета в массиве оттенков
if(_mode==0) x=(clr.x[ArrayMaximum(clr.x,0,3)]+clr.x[ArrayMinimum(clr.x,0,3)])/2; // Светлота= (MAX+MIN)/2  Вариант из https://ru.wikipedia.org/wiki/HSL. Тот вариант к которому изначально стремился Петр.
if(_mode==1) x=(int)round((clr.rgb.b+clr.rgb.g+clr.rgb.r)/3.0); // мой вариант, который мне кажется более логичным и правильным
if(_mode==2) x=127;                                             // То, что в результате получилась у Петра из-за его алгоритмической путаницы. У него цветовой "сгусток" не гуляет, а стоит ровно по центру.
 
Nikolai Semko:

Thank you, Yuri, for your participation and insight, as well as for the clear example. After your changes my code became more pleasant to look at.

But here is a little clarification:

Maybe so :)

int ColorShades(uint _clr,uint& _shades[],int _mode=0)
{
   SuintRGB clr;
   if(ArrayResize(_shades,256)!=256) return(-1);   //ошибка resize()
   clr.clr=_clr; 
   int x;//индекс исходного цвета в массиве оттенков
   if(_mode==0) x=(ArrayMaximum(clr.x,3)+ArrayMinimum(clr.x,3))/2; // Светлота= (MAX+MIN)/2  Вариант из https://ru.wikipedia.org/wiki/HSL. Тот вариант к которому изначально стремился Петр.
   else if(_mode==1) x=(int)round((clr.rgb.b+clr.rgb.g+clr.rgb.r)/3.0); // мой вариант, который мне кажется более логичным и правильным
   else x=127;                                             // То, что в результате получилась у Петра из-за его алгоритмической путаницы. У него цветовой "сгусток" не гуляет, а стоит ровно по центру.
   double d, kr, kg, kb;
   if(x>0)
   {
      //движение от черного
      d=double(x);
      kr=clr.rgb.r/d;          
      kg=clr.rgb.g/d;
      kb=clr.rgb.b/d;
      for(int i=0; i<=x; i++)
      {
         clr.rgb.r=(uchar)fmin(255,kr*i);
         clr.rgb.g=(uchar)fmin(255,kg*i);
         clr.rgb.b=(uchar)fmin(255,kb*i);
         _shades[i]=clr.clr;
      }
   }
   if(x<255)
   {
      //движение от белого
      d=double(255-x);
      kr=(255-clr.rgb.r)/d;
      kg=(255-clr.rgb.g)/d;
      kb=(255-clr.rgb.b)/d;
      for(int i=255, j=0; i>x; i--, j++)
      {
         clr.rgb.r=(uchar)fmax(0,255.0-kr*j);
         clr.rgb.g=(uchar)fmax(0,255.0-kg*j);
         clr.rgb.b=(uchar)fmax(0,255.0-kb*j);
         _shades[i]=clr.clr;
      }
      if(x==0) _shades[0]=0;
   }
   return(x);
}
 
Nikolai Semko:

Do you even realize that code like that

is 100% the same as this one:

After that you shouldn't wonder why your code is much slower.

Well, that's the stupidity of not understanding my algorithm.

 
Yury Kulikov:

Maybe so :)

Yeah.
Only I made a mistake at the beginning and have already corrected it in a previous post

if(_mode==0) x=(clr.x[ArrayMaximum(clr.x,0,3)]+clr.x[ArrayMinimum(clr.x,0,3)])/2;
 
Yury Kulikov:

Is this in line with 99% of the "Windows shades"?

Yes, there is a mistake. Didn't notice it. Reaches white by the middle of the range, although the colour belongs to the grey range. So it should go to white gradually.

 
Nikolai Semko:

...

After that you shouldn't wonder why your code is so much slower.

Generally, it's convenient to take someone else's proven code and, covered by its impeccability, tell others (who are looking for a solution with their minds) how much better you are. Isn't it so, Nicolai?)