Skip to main content

Colors in Bitbybit

Color category icon

View Full Source & Details on GitHub

bitbybit.color builds colors, converts them between the forms the library uses, reads single channels and inverts them.

Two ways to write a color​

  • Hex text, such as #ff5733: six hex digits for red, green and blue. This is what materials and drawing options usually take.
  • An object { r, g, b }, or { r, g, b, a } with an opacity a from 0 (transparent) to 1 (opaque). Its channels run either from 0 to 255 or from 0 to 1, and the functions that convert say which range they read or write.

Giving a color a name​

These functions return their input unchanged. They exist so that a color can be picked once in Rete or Blockly, named and reused:

  • hexColor for hex text;
  • rgb255Color and rgb1Color for { r, g, b } with channels from 0 to 255 or from 0 to 1;
  • rgba255Color (color channels from 0 to 255, a from 0 to 1) and rgba1Color (every channel from 0 to 1).

rgbAtomic255Color and rgbAtomic1Color build an { r, g, b } object from three separate numbers.

Converting​

  • hexToRgb reads hex text into { r, g, b } with channels from 0 to 255: #FF5733 gives { r: 255, g: 87, b: 51 }. The # is optional; anything other than six hex digits, such as the short form #f53, stops the script with an error.
  • hexToRgbMapped does the same and remaps the channels so that 0 becomes from and 255 becomes to. With from 0 and to 1, #FF5733 gives { r: 1, g: 0.341, b: 0.2 }.
  • rgbToHex writes three channel values as hex text, and rgbObjToHex does the same from an { r, g, b } object. min and max say which range the values use; with max 1, channels from 0 to 1 are remapped to 0 to 255 and rounded first, so { r: 1, g: 0.5, b: 0.2 } gives #ff8033.

Reading one channel​

  • getRedParam, getGreenParam and getBlueParam read a channel of a hex color, remapped to the range from to to as hexToRgbMapped does.
  • rgbToRed, rgbToGreen and rgbToBlue read a channel of an { r, g, b } object as it is.

Inverting​

invert turns each channel of a hex color into 255 minus its value, so #FF5733 gives #00a8cc. With blackAndWhite on, it gives black for a light color and white for a dark one instead, judged by perceived brightness, which keeps text readable on a colored background.

Every function​

FunctionWhat it does
getBlueParamReads the blue channel of a hex color, remapped to a range of your choice.
getGreenParamReads the green channel of a hex color, remapped to a range of your choice.
getRedParamReads the red channel of a hex color, remapped to a range of your choice.
hexColorPasses a hex color through unchanged, so a color can be given a name and reused.
hexToRgbReads a hex color into an { r, g, b } object with channels from 0 to 255.
hexToRgbMappedReads a hex color into an { r, g, b } object with the channels remapped to a range of your choice.
invertInverts a hex color, turning each channel into its opposite: 255 minus the value.
rgb1ColorPasses an { r, g, b } color with channels from 0 to 1 through unchanged, so it can be given a name and reused.
rgb255ColorPasses an { r, g, b } color with channels from 0 to 255 through unchanged, so it can be given a name and reused.
rgba1ColorPasses an { r, g, b, a } color with every channel from 0 to 1 through unchanged, so it can be given a name and reused.
rgba255ColorPasses an { r, g, b, a } color with color channels from 0 to 255 and opacity from 0 to 1 through unchanged, so it can be given a name and reused.
rgbAtomic1ColorBuilds an { r, g, b } color from three separate channel values from 0 to 1.
rgbAtomic255ColorBuilds an { r, g, b } color from three separate channel values from 0 to 255.
rgbObjToHexWrites an { r, g, b } color as a hex color.
rgbToBlueReads the blue channel of an { r, g, b } color.
rgbToGreenReads the green channel of an { r, g, b } color.
rgbToHexWrites three channel values as a hex color.
rgbToRedReads the red channel of an { r, g, b } color.