Skip to content
IgorTimofeev edited this page Jan 13, 2019 · 4 revisions

This library is made for color manipulations. It allows you to extrude and pack color channels, convert the RGB color model to HSB and vice versa, perform alpha blending, generate color transitions and convert the color to 8-bit format for the OpenComputers palette.

Contents
Color channels
  color.RGBToInteger
  color.integerToRGB
Color mixing
  color.blend
  color.transition
Color models conversion
  color.RGBToHSB
  color.HSBToRGB
Color compression
  color.to8Bit
  color.to24Bit

Color channels

color.RGBToInteger(8-bit int red, 8-bit int green, 8-bit int blue): 24-bit int result

Packs three color channels and returns a 24-bit color:

color.RGBToInteger(0xFF, 0x00, 0xFF)
> 0xFF00FF

color.integerToRGB(24-bit int color): 8-bit int red, 8-bit int green, 8-bit int blue

Extrudes three color channels from a 24-bit color:

color.integerToRGB(0xFF00FF)
> 255, 0, 255

Color mixing

color.blend(24-bit int firstColor, 24-bit int secondColor, float [0.0; 1.0] secondColorTransparency): 24-bit int result

Mixes two colors considering the transparency of the second color. For example, if you perform this operation on 0xFF0000 (red) and 0x000000 (black) colors with a 0.5 transparency of second one, the result will be 0x7F0000 (dark red):

color.blend(0xFF0000, 0x000000, 0.5)
> 0x7F0000

color.transition(24-bit int firstColor, 24-bit int secondColor, float [0.0; 1.0] position): 24-bit int transitionColor

Generates a transitive color between first and second ones, based on the transition argument, where the value 0.0 is equivalent to the first color, and 1.0 is the second color. For example, if you perform this operation on 0x000000 (black) and 0xFFFFFF (white) colors with transition position equals 0.5, then the result will be 0x7F7F7F (gray):

color.transition(0x000000, 0xFFFFFF, 0.5)
> 0x7F7F7F

Color models conversion

color.RGBToHSB(8-bit int red, 8-bit int green, 8-bit int blue): int [0; 360] hue, float [0.0; 1.0] saturation, float [0.0; 1.0] brightness

Converts three color channels of the RGB color model to the HSB color model and returns the corresponding result:

color.RGBToHSB(0xFF, 0x00, 0x00)
> 360, 1.0, 1.0

For convenience, there is also a method color.integerToHSB(24-bit int value): int [0; 360] hue, float [0.0; 1.0] saturation, float [0.0; 1.0] brightness

color.HSBToRGB(int [0; 360]* hue, float [0.0; 1.0] saturation, float [0.0; 1.0] brightness): 8-bit int red, 8-bit int green, 8-bit int blue

Converts the parameters of the HSB color model into three color channels of the RGB model and returns the corresponding result:

color.HSBToRGB(360, 1.0, 1.0)
> 0xFF, 0xFF, 0xFF

For convenience, there is also a method color.HSBToInteger(int [0; 360]* hue, float [0.0; 1.0] saturation, float [0.0; 1.0] brightness): 24-bit int value

Color compression

color.to8Bit(24-bit int value): 8-bit int value

Looks to 256-color OpenComputers palette and returns the color index that most accurately matches given value using the same search method as in gpu.setBackground(value) do. Returned 8-bit result is very useful for writing to a binary file, allowing you to save extra HDD space. Notice that the method is quite slow, and is not suitable for drawing graphics on the screen in realtime.

color.to24Bit(8-bit int value): 24-bit int value

The method allows you to convert the 8-bit index created by the color.to8Bit method to 24-bit color value. This operation is almost instant in comparison to previous method.