Colors in Bitbybit
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 opacityafrom 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:
hexColorfor hex text;rgb255Colorandrgb1Colorfor{ r, g, b }with channels from 0 to 255 or from 0 to 1;rgba255Color(color channels from 0 to 255,afrom 0 to 1) andrgba1Color(every channel from 0 to 1).
rgbAtomic255Color and rgbAtomic1Color build an { r, g, b } object from three separate numbers.
Converting
hexToRgbreads hex text into{ r, g, b }with channels from 0 to 255:#FF5733gives{ 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.hexToRgbMappeddoes the same and remaps the channels so that 0 becomesfromand 255 becomesto. Withfrom0 andto1,#FF5733gives{ r: 1, g: 0.341, b: 0.2 }.rgbToHexwrites three channel values as hex text, andrgbObjToHexdoes the same from an{ r, g, b }object.minandmaxsay which range the values use; withmax1, 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,getGreenParamandgetBlueParamread a channel of a hex color, remapped to the rangefromtotoashexToRgbMappeddoes.rgbToRed,rgbToGreenandrgbToBlueread 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
| Function | What it does |
|---|---|
getBlueParam | Reads the blue channel of a hex color, remapped to a range of your choice. |
getGreenParam | Reads the green channel of a hex color, remapped to a range of your choice. |
getRedParam | Reads the red channel of a hex color, remapped to a range of your choice. |
hexColor | Passes a hex color through unchanged, so a color can be given a name and reused. |
hexToRgb | Reads a hex color into an { r, g, b } object with channels from 0 to 255. |
hexToRgbMapped | Reads a hex color into an { r, g, b } object with the channels remapped to a range of your choice. |
invert | Inverts a hex color, turning each channel into its opposite: 255 minus the value. |
rgb1Color | Passes an { r, g, b } color with channels from 0 to 1 through unchanged, so it can be given a name and reused. |
rgb255Color | Passes an { r, g, b } color with channels from 0 to 255 through unchanged, so it can be given a name and reused. |
rgba1Color | Passes an { r, g, b, a } color with every channel from 0 to 1 through unchanged, so it can be given a name and reused. |
rgba255Color | Passes 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. |
rgbAtomic1Color | Builds an { r, g, b } color from three separate channel values from 0 to 1. |
rgbAtomic255Color | Builds an { r, g, b } color from three separate channel values from 0 to 255. |
rgbObjToHex | Writes an { r, g, b } color as a hex color. |
rgbToBlue | Reads the blue channel of an { r, g, b } color. |
rgbToGreen | Reads the green channel of an { r, g, b } color. |
rgbToHex | Writes three channel values as a hex color. |
rgbToRed | Reads the red channel of an { r, g, b } color. |