float val; // input 0.0 ~ 1.0 unsigned char R, G, B; // output 0 ~ 255 R = (unsigned char)(( val > 0.5f ? 0.0f : ( val < 0.25f ? 1.0f : (2.0f - 4.0f*val)) ) * 255.0f ); G = (unsigned char)(( val < 0.25f ? 4.0f*val : ( val > 0.75f ? (4.0f - 4.0f*val) : 1.0f ) ) * 255.0f ); B = (unsigned char)(( val < 0.5f ? 0.0f : ( val > 0.75f ? 1.0f : (4.0f*val - 2.0f) )) * 255.0f );