Bitbybit Docs
    Preparing search index...

    Class Color

    Colors in the two forms the library uses: a hex text such as #ff5733, and an object { r, g, b } or { r, g, b, a } whose channels run either from 0 to 255 or from 0 to 1. The methods here build colors in each form, convert between them with an explicit channel range, read single channels and invert a color.

    Index

    Constructors

    convert

    • Reads a hex color into an { r, g, b } object with channels from 0 to 255.

      The text may start with or without #; anything else than six hex digits throws an error. Example: '#FF5733' -> { r: 255, g: 87, b: 51 }

      Parameters

      Returns ColorRGB

      The color object with channels from 0 to 255

      const rgb = bitbybit.color.hexToRgb({ color: "#ff5733" });
      
    • Writes three channel values as a hex color.

      min and max say which range the values use; a range other than 0 to 255 is remapped first, so channels from 0 to 1 work as well. Example: r 255, g 87, b 51 in [0,255] -> '#ff5733'; r 1, g 0.5, b 0.2 in [0,1] -> '#ff8033'

      Parameters

      • inputs: RGBMinMaxDto

        The red, green and blue values and their range

      Returns string

      The hex color

      const hex = bitbybit.color.rgbToHex({ r: 1, g: 0.5, b: 0.2, min: 0, max: 1 });
      
    • Writes an { r, g, b } color as a hex color.

      min and max say which range the channels use; a range other than 0 to 255 is remapped first. Example: { r: 1, g: 0.5, b: 0.2 } in [0,1] -> '#ff8033'

      Parameters

      Returns string

      The hex color

      const hex = bitbybit.color.rgbObjToHex({ rgb: { r: 1, g: 0.5, b: 0.2 }, min: 0, max: 1 });
      
    • Reads a hex color into an { r, g, b } object with the channels remapped to a range of your choice.

      Example: '#FF5733' mapped to [0,1] -> { r: 1, g: 0.341, b: 0.2 }

      Parameters

      • inputs: HexDtoMapped

        The hex color and the range to map the channels to

      Returns ColorRGB

      The color object with channels in that range

      const rgb = bitbybit.color.hexToRgbMapped({ color: "#ff5733", from: 0, to: 1 });
      

    create

    • Passes a hex color through unchanged, so a color can be given a name and reused.

      Example: '#FF5733' -> '#FF5733'

      Parameters

      Returns string

      The same hex color

    • Passes an { r, g, b } color with channels from 0 to 255 through unchanged, so it can be given a name and reused.

      Example: { r: 255, g: 87, b: 51 } -> the same object

      Parameters

      Returns ColorRGB

      The same color object

    • Passes an { r, g, b } color with channels from 0 to 1 through unchanged, so it can be given a name and reused.

      Example: { r: 1, g: 0.34, b: 0.2 } -> the same object

      Parameters

      Returns ColorRGB

      The same color object

    • 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.

      Example: { r: 255, g: 87, b: 51, a: 1 } -> the same object

      Parameters

      Returns ColorRGBA

      The same color object

    • 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.

      Example: { r: 1, g: 0.34, b: 0.2, a: 1 } -> the same object

      Parameters

      Returns ColorRGBA

      The same color object

    • Builds an { r, g, b } color from three separate channel values from 0 to 255.

      Example: r 255, g 87, b 51 -> { r: 255, g: 87, b: 51 }

      Parameters

      Returns ColorRGB

      The color object

      const orange = bitbybit.color.rgbAtomic255Color({ r: 255, g: 87, b: 51 });
      
    • Builds an { r, g, b } color from three separate channel values from 0 to 1.

      Example: r 1, g 0.34, b 0.2 -> { r: 1, g: 0.34, b: 0.2 }

      Parameters

      Returns ColorRGB

      The color object

      const orange = bitbybit.color.rgbAtomic1Color({ r: 1, g: 0.34, b: 0.2 });
      

    hex to

    • Reads the red channel of a hex color, remapped to a range of your choice.

      Example: '#FF5733' in [0,1] -> 1

      Parameters

      • inputs: HexDtoMapped

        The hex color and the range to map the channel to

      Returns number

      The red channel in that range

      const red = bitbybit.color.getRedParam({ color: "#ff5733", from: 0, to: 1 });
      
    • Reads the green channel of a hex color, remapped to a range of your choice.

      Example: '#FF5733' in [0,1] -> 0.341

      Parameters

      • inputs: HexDtoMapped

        The hex color and the range to map the channel to

      Returns number

      The green channel in that range

      const green = bitbybit.color.getGreenParam({ color: "#ff5733", from: 0, to: 1 });
      
    • Reads the blue channel of a hex color, remapped to a range of your choice.

      Example: '#FF5733' in [0,1] -> 0.2

      Parameters

      • inputs: HexDtoMapped

        The hex color and the range to map the channel to

      Returns number

      The blue channel in that range

      const blue = bitbybit.color.getBlueParam({ color: "#ff5733", from: 0, to: 1 });
      
    • Inverts a hex color, turning each channel into its opposite: 255 minus the value.

      With blackAndWhite on, the result is plain black for a light color or white for a dark one, which suits text on a colored background. Example: '#FF5733' -> '#00a8cc'; with blackAndWhite -> '#ffffff'

      Parameters

      • inputs: InvertHexDto

        The hex color and whether to reduce the result to black or white

      Returns string

      The inverted hex color

      const textColor = bitbybit.color.invert({ color: "#ff5733", blackAndWhite: true });
      

    rgb to

    • Reads the red channel of an { r, g, b } color.

      Example: { r: 255, g: 87, b: 51 } -> 255

      Parameters

      Returns number

      The red channel, in whatever range the object uses

    • Reads the green channel of an { r, g, b } color.

      Example: { r: 255, g: 87, b: 51 } -> 87

      Parameters

      Returns number

      The green channel, in whatever range the object uses

    • Reads the blue channel of an { r, g, b } color.

      Example: { r: 255, g: 87, b: 51 } -> 51

      Parameters

      Returns number

      The blue channel, in whatever range the object uses