Mapping an Integer to an RGB color in C#

后端 未结 3 834
挽巷
挽巷 2020-12-29 10:47

So right now I have a number between 0 and 2^24, and I need to map it to three RGB values. I\'m having a bit of trouble on how I\'d accomplish this. Any assistance is apprec

3条回答
  •  一生所求
    2020-12-29 10:55

    You can use the BitConverter class to get the bytes from the int:

    byte[] values = BitConverter.GetBytes(number);
    if (!BitConverter.IsLittleEndian) Array.Reverse(values);
    

    The array will have four bytes. The first three bytes contain your number:

    byte b = values[0];
    byte g = values[1];
    byte r = values[2];
    

提交回复
热议问题