// Delphi version // Opracował Witold J.Janik; WJJ@CAD.PL // *** // Funkcja FnTecza umożliwia narysowanie // tęczy przy zmianie [i] od [iMin] do [iMax] // podziękowania dla Andrzeja Wąsika z [pl.comp.lang.delphi] function FnTecza(iMin, iMax, i: Integer): TColor; var m: Double; r, g, b, mt: Byte; begin m := (i - iMin)/(iMax - iMin + 1) * 6; mt := (round(frac(m)*$FF)); case Trunc(m) of 0: begin R := $FF; G := mt; B := 0; end; 1: begin R := $FF - mt; G := $FF; B := 0; end; 2: begin R := 0; G := $FF; B := mt; end; 3: begin R := 0; G := $FF - mt; B := $FF; end; 4: begin R := mt; G := 0; B := $FF; end; 5: begin R := $ff; G := 0; B := $FF - mt; end; end; // case Result := rgb(R,G,B); end; ///// ================================= // C++ version // here are some my modification but the main code is the same // as in Witold J.Janik code // Uint32 GiveRainbowColor(double position) // this function gives 1D linear RGB color gradient // color is proportional to position // position <0;1> // position means position of color in color gradient { if (position>1)position=position-int(position); // if position > 1 then we have repetition of colors // it maybe useful Uint8 R, G, B;// byte int nmax=6;// number of color bars double m=nmax* position; int n=int(m); // integer of m double f=m-n; // fraction of m Uint8 t=int(f*255); switch( n){ case 0: { R = 255; G = t; B = 0; break; }; case 1: { R = 255 - t; G = 255; B = 0; break; }; case 2: { R = 0; G = 255; B = t; break; }; case 3: { R = 0; G = 255 - t; B = 255; break; }; case 4: { R = t; G = 0; B = 255; break; }; case 5: { R = 255; G = 0; B = 255 - t; break; }; }; // case return (R << 16) | (G << 8) | B; } //------------------------- /* ============ C version ========================= */ void GiveRainbowColor(double position,unsigned char c[]) { /* if position > 1 then we have repetition of colors it maybe useful */ if (position>1.0){if (position-(int)position==0.0)position=1.0; else position=position-(int)position;} unsigned char nmax=6; /* number of color bars */ double m=nmax* position; int n=(int)m; // integer of m double f=m-n; // fraction of m unsigned char t=(int)(f*255); switch( n){ case 0: { c[0] = 255; c[1] = t; c[2] = 0; break; }; case 1: { c[0] = 255 - t; c[1] = 255; c[2] = 0; break; }; case 2: { c[0] = 0; c[1] = 255; c[2] = t; break; }; case 3: { c[0] = 0; c[1] = 255 - t; c[2] = 255; break; }; case 4: { c[0] = t; c[1] = 0; c[2] = 255; break; }; case 5: { c[0] = 255; c[1] = 0; c[2] = 255 - t; break; }; default: { c[0] = 255; c[1] = 0; c[2] = 0; break; }; }; // case } /* =====================================*/ // fraktal.republika.pl // Adam Majewski 2008.03