RGB to HSL

RGB Input
Sample
HSL Output

RGB to HSL Converter: Convert RGB Color Codes to HSL Color Codes

Are you looking for an easy and efficient way to convert RGB color codes to HSL color codes? Look no further than RGB to HSL Converter. Our online tool is free, fast, and secure, and requires no system or software dependencies.

Features

  • Free and online tool
  • No system or software dependencies
  • Clear and Copy button
  • Sample input
  • Data security through local computing

Introduction

RGB to HSL Converter is an online tool that allows users to convert RGB color codes to HSL color codes. RGB (red, green, blue) and HSL (hue, saturation, lightness) are two different ways of representing colors, and RGB to HSL Converter makes it easy to convert between them.

Benefits and Advantages

The main advantage of using RGB to HSL Converter is that it simplifies the process of converting RGB color codes to HSL color codes. The tool is free and online, so users can access it from anywhere with an internet connection. Additionally, the tool is fast and secure, making it a reliable choice for anyone who needs to convert RGB color codes to HSL color codes.

The Core Algorithm

The core algorithm used by RGB to HSL Converter is straightforward and easy to understand. It involves a simple set of calculations to convert RGB color codes to HSL color codes. The formula for converting RGB to HSL is as follows:

  • To calculate H:
    • Find the maximum and minimum values among R, G, and B.
    • H is then calculated using the following formula:
    if (max == min) H = 0
    else if (max == R) H = 60 _ ((G - B) / (max - min))
    else if (max == G) H = 60 _ ((B - R) / (max - min)) + 120
    else if (max == B) H = 60 \* ((R - G) / (max - min)) + 240
    if (H < 0) H += 360
  • To calculate S:
    • If max and min are both 0, S is 0.
    • Otherwise, S is calculated as (max - min) / (1 - abs(max + min - 1))
  • To calculate L:
    • L is simply the average of max and min.

How to Use RGB to HSL Converter

Using RGB to HSL Converter is easy and straightforward. Here's a step-by-step guide:

  1. Input or paste the RGB color code you want to convert into the tool.
  2. Click the "Convert" button to convert the RGB color code to HSL.
  3. The HSL color code will be displayed on the screen.
  4. Copy the HSL color code to your clipboard by clicking the "Copy" button.

That's it! With just a few clicks, you can convert any RGB color code to its corresponding HSL color code.

Examples

Python

def rgb_to_hsl(r, g, b):
    r, g, b = r/255.0, g/255.0, b/255.0
    max_val = max(r, g, b)
    min_val = min(r, g, b)
    h, s, l = 0, 0, ((max_val + min_val) / 2)

    if max_val == min_val:
        h = s = 0
    else:
        diff = max_val - min_val
        s = diff / (2 - max_val - min_val)
        if max_val == r:
            h = (g - b) / diff + (g < b and 6 or 0)
        elif max_val == g:
            h = (b - r) / diff + 2
        else:
            h = (r - g) / diff + 4
        h /= 6

    return (int(h*360), int(s*100), int(l*100))

C

#include <stdio.h>

void ConvertRGBtoHSL(int r, int g, int b)
{
    float fr = r / 255.0f;
    float fg = g / 255.0f;
    float fb = b / 255.0f;
    float max = fr > fg ? (fr > fb ? fr : fb) : (fg > fb ? fg : fb);
    float min = fr < fg ? (fr < fb ? fr : fb) : (fg < fb ? fg : fb);
    float h = 0, s = 0, l = (max + min) / 2.0;
    if (max == min) {
        h = 0;
        s = 0;
    }
    else {
        float d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        if (max == fr) {
            h = (fg - fb) / d + (fg < fb ? 6 : 0);
        }
        else if (max == fg) {
            h = (fb - fr) / d + 2;
        }
        else if (max == fb) {
            h = (fr - fg) / d + 4;
        }
        h /= 6;
    }
    printf("HSL: %.0f, %.0f%%, %.0f%%\n", h * 360, s * 100, l * 100);
}

JavaScript

function rgbToHsl(r, g, b) {
  (r /= 255), (g /= 255), (b /= 255);
  let max = Math.max(r, g, b),
    min = Math.min(r, g, b);
  let h,
    s,
    l = (max + min) / 2;

  if (max == min) {
    h = s = 0;
  } else {
    let d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0);
        break;
      case g:
        h = (b - r) / d + 2;
        break;
      case b:
        h = (r - g) / d + 4;
        break;
    }
    h /= 6;
  }
  return [h * 360, s * 100, l * 100];
}

Java

public static void rgbToHsl(int r, int g, int b) {
    float fr = r / 255.0f;
    float fg = g / 255.0f;
    float fb = b / 255.0f;
    float max = Math.max(fr, Math.max(fg, fb));
    float min = Math.min(fr, Math.min(fg, fb));
    float h = 0, s = 0, l = (max + min) / 2;
    if (max == min) {
        h = 0;
        s = 0;
    } else {
        float d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        if (max == fr) {
            h = (fg - fb) / d + (fg < fb ? 6 : 0);
        } else if (max == fg) {
            h = (fb - fr) / d + 2;
        } else if (max == fb) {
            h = (fr - fg) / d + 4;
        }
        h /= 6;
    }
    System.out.printf("HSL: %.0f, %.0f%%, %.0f%%\n", h * 360, s * 100, l * 100);
}

PHP

function RGB2HSL($R, $G, $B) {
    $R = ($R / 255);
    $G = ($G / 255);
    $B = ($B / 255);

    $maxValue = max($R, $G, $B);
    $minValue = min($R, $G, $B);
    $delta = $maxValue - $minValue;

    $L = ($maxValue + $minValue) / 2;
    $H = 0;
    $S = 0;

    if ($delta == 0) {
        $H = 0;
        $S = 0;
    } else {
        if ($L < 0.5) {
            $S = $delta / ($maxValue + $minValue);
        } else {
            $S = $delta / (2 - $maxValue - $minValue);
        }

        $deltaR = ((($maxValue - $R) / 6) + ($delta / 2)) / $delta;
        $deltaG = ((($maxValue - $G) / 6) + ($delta / 2)) / $delta;
        $deltaB = ((($maxValue - $B) / 6) + ($delta / 2)) / $delta;

        if ($R == $maxValue) {
            $H = $deltaB - $deltaG;
        } else if ($G == $maxValue) {
            $H = (1 / 3) + $deltaR - $deltaB;
        } else if ($B == $maxValue) {
            $H = (2 / 3) + $deltaG - $deltaR;
        }

        if ($H < 0) {
            $H++;
        }
        if ($H > 1) {
            $H--;
        }
    }
    return [(int)($H * 360), (int)($S * 100), (int)($L * 100)];
}

Conclusion

RGB to HSL Converter is an easy-to-use and efficient tool for converting RGB color codes to HSL color codes. With its free, online, and secure design, anyone can use the tool to convert colors quickly and easily. Whether you're a designer, developer, or just someone who needs to convert colors, RGB to HSL Converter is the perfect tool for the job.

Frequently Asked Questions (FAQ)

Meet our more Tools