Javascript: Color model conversion

JS: Functions
Last update: 13 Sep 23:14

Hexadecimal numbers are used in HTML and CSS to specify colors. The grid sign # is put in front of the hexadecimal number, for example, #135278, to prevent confusion when determining the numeral system. The color designation (rrggbb) is broken up into three parts, with the first two symbols designating the color's red component, the middle two symbols its green component, and the last two symbols its blue component. In the hexadecimal system, each of the three colors - red, green, and blue — can thus take values ranging from 00 to FF.


When working with colors, it is often required to obtain separate values of the red, green and blue (RGB) color components in decimal and vice versa. Implement and export the functions rgbToHex() and hexToRgb(), which return the corresponding color model.


hexToRgb('#24ab00'); // { r: 36, g: 171, b: 0 }

rgbToHex(36, 171, 0); // '#24ab00'


  • You may find of use for the chunk function from the lodash library
  • Use the parseInt() function to convert the string into the required numeral system
  • Explore the possibilities of the toString() method for a number, look at examples
  • Additionally, you can use the padStart() method

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory