{"_id":"@acca-alex-22/uber-color","_rev":"2-eb4fa4c81dfe206849d5d12f445e4229","name":"@acca-alex-22/uber-color","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@acca-alex-22/uber-color","version":"1.0.0","author":{"name":"acca-alex-22"},"license":"ISC","_id":"@acca-alex-22/uber-color@1.0.0","maintainers":[{"name":"acca-alex-22","email":"dev.hugomartinez@gmail.com"}],"homepage":"https://github.com/halex22/uber-color#readme","bugs":{"url":"https://github.com/halex22/uber-color/issues"},"dist":{"shasum":"d18261d1b96c1d50eedf8f1d158f4ea1ee5f22b7","tarball":"https://registry.npmjs.org/@acca-alex-22/uber-color/-/uber-color-1.0.0.tgz","fileCount":64,"integrity":"sha512-O8CRbvOmGGDDa+cEItv9n6MQK7zTmhmBBS5M4jS6NOJZt05uYQpBwR/LpBW0n82WmOfHIHmxAsUAP5syhsVvPw==","signatures":[{"sig":"MEUCIQDbJZlWh/ZsFNW5zaTxJ8y6BvbiMkeLbP+1eFlRg2PHfAIgVFsDgFY4aJhoWy7yzXLeJ0v68tpPFTJuhCT+z0gDCpE=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":2125360},"main":"index.js","type":"module","gitHead":"05f7e41828d6f6655d1c29f9364ef29c72baf1d4","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"acca-alex-22","email":"dev.hugomartinez@gmail.com"},"repository":{"url":"git+https://github.com/halex22/uber-color.git","type":"git"},"_npmVersion":"10.5.0","description":"Uber Color uses a Class named UColor to generate color palettes, contract colors, grayScale from a rgb color","directories":{},"_nodeVersion":"20.12.2","_hasShrinkwrap":false,"devDependencies":{"jsdoc":"^4.0.4"},"_npmOperationalInternal":{"tmp":"tmp/uber-color_1.0.0_1741360460331_0.09075743625485777","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@acca-alex-22/uber-color","version":"1.0.1","description":"Uber Color uses a Class named UColor to generate color palettes, contract colors, grayScale from a rgb color","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git+https://github.com/halex22/uber-color.git"},"author":{"name":"acca-alex-22"},"license":"ISC","type":"module","devDependencies":{"jsdoc":"^4.0.4"},"_id":"@acca-alex-22/uber-color@1.0.1","gitHead":"7a5f6a489a9745fa6c42f34bee02b5ea8451dd01","bugs":{"url":"https://github.com/halex22/uber-color/issues"},"homepage":"https://github.com/halex22/uber-color#readme","_nodeVersion":"22.14.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-PnX1n1OB1RnLsXEvxhLPZGvAbACzF+ak9MPfYzI1wuw82VaA5YeAUmaLL6HoRuO5z+3FPk7GbAl9BPLuIc4+0g==","shasum":"f1cbeb30ce22da4875a5ad66cf5005e9a5fe42b1","tarball":"https://registry.npmjs.org/@acca-alex-22/uber-color/-/uber-color-1.0.1.tgz","fileCount":35,"unpackedSize":1094982,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCtbmW6A6Y/fAtE1o1GL1/C0D0vDacyUWWyWpQPQP6hcgIgbqeKa+rpRE2/77frcFdBflimzPe9/nEvGyTk7JBi1y8="}]},"_npmUser":{"name":"acca-alex-22","email":"dev.hugomartinez@gmail.com"},"directories":{},"maintainers":[{"name":"acca-alex-22","email":"dev.hugomartinez@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/uber-color_1.0.1_1741777656433_0.6142591754092472"},"_hasShrinkwrap":false}},"time":{"created":"2025-03-07T15:14:20.262Z","modified":"2025-03-12T11:07:36.759Z","1.0.0":"2025-03-07T15:14:20.560Z","1.0.1":"2025-03-12T11:07:36.603Z"},"bugs":{"url":"https://github.com/halex22/uber-color/issues"},"author":{"name":"acca-alex-22"},"license":"ISC","homepage":"https://github.com/halex22/uber-color#readme","repository":{"type":"git","url":"git+https://github.com/halex22/uber-color.git"},"description":"Uber Color uses a Class named UColor to generate color palettes, contract colors, grayScale from a rgb color","maintainers":[{"name":"acca-alex-22","email":"dev.hugomartinez@gmail.com"}],"readme":"# Uber Color Package\r\n\r\n The `UColor` class in JavaScript provides methods for handling RGB and RGBA color values, including\r\nconversion between different color formats, generating random colors, calculating grayscale values,\r\ncreating color palettes, and determining contrasting colors. */\r\nexport default class UColor {\r\n\r\n\r\n   * The constructor function initializes an object with RGB color values and an optional alpha value.\r\n   * @param {number} r - The parameter `r` represents the red component of a color in the RGB color model.\r\n   * @param {number} g - The parameter `g` in the constructor function represents the green component of a color\r\n   * in RGB (Red, Green, Blue) format. It is used to define the intensity of the green color in the\r\n   * overall color.\r\n   * @param {number} b - The parameter `b` in the constructor function represents the blue component of a color\r\n   * in RGB (Red, Green, Blue) color model. It is used to define the intensity of the blue color in the\r\n   * color being created.\r\n   * @param {number?} [a=1] - The parameter `a` in the constructor function represents the alpha value of the\r\n   * color, which determines the opacity. It is optional and defaults to 1 if not provided.\r\n   */\r\n  constructor(r, g, b, a = 1) {\r\n    this.r = r;\r\n    this.g = g;\r\n    this.b = b;\r\n    this.a = a;\r\n    this.colorsArray = [r, g, b];\r\n  }\r\n\r\n\r\n   * The function `fromHexCode` converts a hexadecimal color code to an array of RGBA values.\r\n   * @param {string} hexStr - The `hexStr` parameter in the `fromHexCode` function is a hexadecimal color code\r\n   * represented as a string. It may start with a \"#\" symbol followed by six hexadecimal characters\r\n   * representing the RGB values of a color. For example, \"#FFA500\" represents the color orange.\r\n   * @returns {UColor}The function `fromHexCode` is returning a new instance of `UColor` class with parsed\r\n   * values from the input hexadecimal string `hexStr`. The parsed values are obtained by converting\r\n   * each pair of characters in the hexadecimal string to their decimal equivalent using base 16. If\r\n   * the fourth parsed value exists, it is then normalized to a range between 0 and 1 by dividing it by\r\n   * 255\r\n   \r\n  static fromHexCode(hexStr) {\r\n    const cleanedHexStr = hexStr.slice(1);\r\n    const arrayOfStr = [...cleanedHexStr];\r\n    const groupOfCouples = [];\r\n\r\n    while (arrayOfStr.length > 0) {\r\n      const coupleStrArray = arrayOfStr.splice(0, 2);\r\n      groupOfCouples.push(coupleStrArray.join(\"\"));\r\n    }\r\n\r\n    const parsedCouples = groupOfCouples.map((couple) => parseInt(couple, 16));\r\n    if (parsedCouples[3]) {\r\n      parsedCouples[3] = Math.round((parsedCouples[3] / 255) * 100) / 100;\r\n    }\r\n\r\n    return new UColor(...parsedCouples);\r\n  }\r\n\r\n\r\n  \r\n   * The function `random` generates a random color with an optional alpha value.\r\n   * @param {number} alpha - The `alpha` parameter in the `random` function is used to specify the alpha\r\n   * (transparency) value of the color. If a value is provided for `alpha`, it will be used as the\r\n   * alpha value for the color. If no value is provided for `alpha`, a random alpha value\r\n   * @returns {UColor} A new `UColor` object is being returned, with the RGB values generated by the\r\n   * `randomBetween()` function for red, green, and blue channels, and the alpha value either provided\r\n   * as an argument or generated by the `randomAlpha()` function.\r\n   \r\n  static random(alpha) {\r\n    const colors = [randomBetween(), randomBetween(), randomBetween()];\r\n    colors.push(alpha ? alpha : randomAlpha());\r\n    return new UColor(...colors);\r\n  }\r\n\r\n\r\n   * The function `fromRGB` takes a string in the format `rgba(r,g,b,a)` and returns a new `UColor`\r\n   * object with the corresponding color values.\r\n   * @param {string} RGBStr - The `RGBStr` parameter is a string representing an RGB color in the format\r\n   * `rgba(r,g,b,a)`, where:\r\n   * @returns {UColor} An instance of the `UColor` class with the RGB values extracted from the `RGBStr` string.\r\n   */\r\n  static fromRGB(RGBStr) {\r\n    const splittedArray = RGBStr.split(\"(\");\r\n    const colorArraysStr = splittedArray[1].split(\")\")[0].split(\",\");\r\n    const colorsArrayInt = colorArraysStr.map((color) => parseFloat(color));\r\n    return new UColor(...colorsArrayInt);\r\n  }\r\n\r\n\r\n\r\n * The `toHex` function converts RGBA values to a hexadecimal color code.\r\n * @returns The `toHex()` method is returning a hexadecimal representation of the color values (red,\r\n * green, blue, alpha) in the format `#RRGGBBAA`, where RR represents the red value, GG represents the\r\n * green value, BB represents the blue value, and AA represents the alpha value.\r\n */\r\n  toHex() {\r\n    const r = this.r.toString(16).padStart(2, \"0\");\r\n    const g = this.g.toString(16).padStart(2, \"0\");\r\n    const b = this.b.toString(16).padStart(2, \"0\");\r\n    const a = Math.round(this.a * 255)\r\n      .toString(16)\r\n      .padStart(2, \"0\");\r\n    return `#${r}${g}${b}${a}`;\r\n  }\r\n\r\n  /**\r\n   * The `toRGB()` function returns the RGBA color values in a string format.\r\n   * @returns {string} The `toRGB()` method is returning a string in the format `rgba(r, b, g, a)` where `r`,\r\n   * `b`, `g`, and `a` are the values of the red, blue, green, and alpha components respectively of the\r\n   * object calling the method.\r\n   */\r\n  toRGB() {\r\n    return `rgba(${this.r},${this.b},${this.g}, ${this.a})`;\r\n  }\r\n\r\n\r\n\r\n   * The function `getGrayColor` calculates the grayscale value of a color based on its RGB values.\r\n   * @returns {UColor} A new `UColor` object is being returned with the RGB values converted to a grayscale\r\n   * color. The RGB values are calculated based on the formula `gray = red * 0.3 + green * 0.59 + blue\r\n   * * 0.11`, and then the grayscale value is applied to all RGB components while keeping the alpha\r\n   * value unchanged.\r\n   */\r\n  getGrayColor() {\r\n    const red = Math.round(this.r * 0.3);\r\n    const green = Math.round(this.g * 0.59);\r\n    const blue = Math.round(this.b * 0.11);\r\n\r\n    const gray = red + green + blue;\r\n\r\n    const arrayGrey = [gray, gray, gray, this.a];\r\n\r\n    return new UColor(...arrayGrey);\r\n  }\r\n\r\n\r\n  /**\r\n   * The function `getPalette` generates a color palette based on the input colors by adjusting the hue\r\n   * values.\r\n   * @returns The `getPalette()` function returns an array called `Palette` containing three UColor\r\n   * objects. The first UColor object is created using the initial colorsArray, and the next two UColor\r\n   * objects are created by modifying the hue value of the initial color and converting it to RGB using\r\n   * the hslToRgb function.\r\n   */\r\n  getPalette() {\r\n    const hslVersion = rgbToHsl(...this.colorsArray);\r\n    const hue = hslVersion[0];\r\n    const s = hslVersion[1];\r\n    const l = hslVersion[2];\r\n    const Palette = [new UColor(this.colorsArray)]\r\n    const movements = 120\r\n\r\n    for (let i = 0; i &lt; 2; i++) {\r\n      const newHue = (hue + (movements * (1 + i))) % 360;\r\n      const newHsl = [newHue, s, l]\r\n      const newColor  = hslToRgb(...newHsl)\r\n      Palette.push(new UColor(...newColor))\r\n    }\r\n    \r\n\r\n    return Palette\r\n  }\r\n\r\n\r\n\r\n  /**\r\n   * The function `getContrastColor` calculates a contrasting color based on the input RGB color array.\r\n   * @returns {UColor} A new `UColor` object with the RGB values of the color that is the contrast of the\r\n   * original color.\r\n   */\r\n  getContrastColor() {\r\n\r\n    const hslVersion = rgbToHsl(...this.colorsArray);\r\n    const hue = hslVersion[0];\r\n    const newHue = (hue + 180) % 360;\r\n    const l = hslVersion[2];\r\n    let newLuminosity = l\r\n    if (l &lt; 20 || l > 80) {\r\n      newLuminosity = 100 - l;\r\n    }\r\n    const newHsl = [newHue, hslVersion[1], newLuminosity]\r\n    const contrastedRGB = hslToRgb(...newHsl)\r\n    return new UColor(...contrastedRGB)\r\n  }\r\n\r\n}\r\n\r\n\r\n/**\r\n * The function `randomBetween` generates a random number between the values of `MIN_COLOR` and\r\n * `MAX_COLOR`.\r\n * @returns The function `randomBetween` returns a random number between the values of `MIN_COLOR` and\r\n * `MAX_COLOR`, inclusive.\r\n */\r\nexport function randomBetween () {\r\n  return MIN_COLOR + Math.floor(Math.random() * (MAX_COLOR - MIN_COLOR + 1));\r\n}\r\n  \r\n\r\n/**\r\n * The function randomAlpha generates a random decimal number between 0 and 1.\r\n * @returns The function `randomAlpha` returns a random number between 0 and 1 with two decimal places.\r\n */\r\nexport function randomAlpha () {\r\n  return Math.round(Math.random() * 100) / 100;\r\n}\r\n\r\n\r\n\r\n\r\n\r\n/**\r\n *The `rgbToHsl` function is converting RGB (Red, Green, Blue) values to HSL (Hue, Saturation, Luminosity) values. Here's a breakdown of what the function is doing:\r\n * @param {number} red \r\n * @param {number} green \r\n * @param {number} blue \r\n * @returns {number[]}\r\n */\r\nexport function rgbToHsl (r, g, b) {\r\n  r /= 255;\r\n  g /= 255;\r\n  b /= 255;\r\n  const l = Math.max(r, g, b);\r\n  const s = l - Math.min(r, g, b);\r\n  const h = s\r\n    ? l === r\r\n      ? (g - b) / s\r\n      : l === g\r\n      ? 2 + (b - r) / s\r\n      : 4 + (r - g) / s\r\n    : 0;\r\n  return [\r\n    Math.round(60 * h &lt; 0 ? 60 * h + 360 : 60 * h),\r\n    Math.round(\r\n      100 * (s ? (l &lt;= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0)\r\n    ),\r\n    Math.round((100 * (2 * l - s)) / 2),\r\n  ];\r\n};\r\n\r\n\r\n/**\r\n * The function `hslToRgb` converts HSL (Hue, Saturation, Luminosity) values to RGB (Red, Green, Blue)\r\n * values in JavaScript.\r\n * @param {number} hue - The `hue` parameter in the `hslToRgb` function represents the hue value of the color in\r\n * the HSL (Hue, Saturation, Lightness) color model. Hue is a degree on the color wheel from 0 to 360,\r\n * where 0 (or 360\r\n * @param {number} saturate - The `saturate` parameter in the `hslToRgb` function represents the saturation\r\n * value of the HSL (Hue, Saturation, Lightness) color model. It indicates the intensity or purity of\r\n * the color. In the function, the `saturate` value is divided\r\n * @param {number} luminosity - The `luminosity` parameter in the `hslToRgb` function represents the lightness\r\n * or darkness of the color. It ranges from 0 to 100, where 0 is black and 100 is white. A value of 50\r\n * represents the normal color.\r\n * @returns {number[]}The function `hslToRgb` is returning an array of three values representing the RGB color\r\n * values calculated from the input HSL (Hue, Saturation, Luminosity) values. The array contains the\r\n * red, green, and blue values respectively.\r\n */\r\nexport function hslToRgb (h, s, l) {\r\n  s /= 100;\r\n  l /= 100;\r\n  const k = (n) => (n + h / 30) % 12;\r\n  const a = s * Math.min(l, 1 - l);\r\n  const f = (n) =>\r\n    l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));\r\n  return [\r\n    Math.round(255 * f(0)),\r\n    Math.round(255 * f(8)),\r\n    Math.round(255 * f(4)),\r\n  ];\r\n}","readmeFilename":"README.md"}