[javascript] RGB to hex and hex to RGB

I made a small Javascript color class for RGB and Hex colors, this class also includes RGB and Hex validation functions. I've added the code as a snippet to this answer.

_x000D_
_x000D_
var colorClass = function() {_x000D_
   this.validateRgb = function(color) {_x000D_
      return typeof color === 'object' &&_x000D_
      color.length === 3               &&_x000D_
      Math.min.apply(null, color) >= 0 &&_x000D_
      Math.max.apply(null, color) <= 255;_x000D_
   };_x000D_
   this.validateHex = function(color) {_x000D_
      return color.match(/^\#?(([0-9a-f]{3}){1,2})$/i);_x000D_
   };_x000D_
   this.hexToRgb = function(color) {_x000D_
      var hex    = color.replace(/^\#/, '');_x000D_
      var length = hex.length;_x000D_
      return     [_x000D_
         parseInt(length === 6 ? hex['0'] + hex['1'] : hex['0'] + hex['0'], 16),_x000D_
         parseInt(length === 6 ? hex['2'] + hex['3'] : hex['1'] + hex['1'], 16),_x000D_
         parseInt(length === 6 ? hex['4'] + hex['5'] : hex['2'] + hex['2'], 16)_x000D_
      ];_x000D_
   };_x000D_
   this.rgbToHex = function(color) {_x000D_
      return '#' +_x000D_
      ('0' + parseInt(color['0'], 10).toString(16)).slice(-2) +_x000D_
      ('0' + parseInt(color['1'], 10).toString(16)).slice(-2) +_x000D_
      ('0' + parseInt(color['2'], 10).toString(16)).slice(-2);_x000D_
   };_x000D_
};_x000D_
_x000D_
var colors = new colorClass();_x000D_
console.log(colors.hexToRgb('#FFFFFF'));//       [255, 255, 255]_x000D_
console.log(colors.rgbToHex([255, 255, 255]));// #FFFFFF
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to colors

is it possible to add colors to python output? How do I use hexadecimal color strings in Flutter? How do I change the font color in an html table? How do I print colored output with Python 3? Change bar plot colour in geom_bar with ggplot2 in r How can I color a UIImage in Swift? How to change text color and console color in code::blocks? Android lollipop change navigation bar color How to change status bar color to match app in Lollipop? [Android] How to change color of the back arrow in the new material theme?

Examples related to hex

Transparent ARGB hex value How to convert a hex string to hex number Javascript: Unicode string to hex Converting Hexadecimal String to Decimal Integer Convert string to hex-string in C# Print a variable in hexadecimal in Python Convert ascii char[] to hexadecimal char[] in C Hex transparency in colors printf() formatting for hex Python Hexadecimal

Examples related to rgb

What are the RGB codes for the Conditional Formatting 'Styles' in Excel? libpng warning: iCCP: known incorrect sRGB profile Get pixel's RGB using PIL How to compare two colors for similarity/difference Given an RGB value, how do I create a tint (or shade)? Why rgb and not cmy? RGB to hex and hex to RGB Convert System.Drawing.Color to RGB and Hex Value HSL to RGB color conversion How do I write a RGB color value in JavaScript?