I am looking for a tool or the algorithm to convert between HSL color to RGB. It seems to me that HSL is not very widely used so I am not having much luck searching for a converter.
相关问题
- DBGrid - How to set an individual background color
- How to display an image represented by three matri
- How to delete a certain part of an Image?
- draw polylines with different colors on v2 maps
- How do I change the color of the navigation bar ic
相关文章
- Emacs/xterm color annoyance on Linux
- matplotlib bwr-colormap, always centered on zero
- MeshLab: How to import XYZRGB file
- How to add RGB values into setColor() in Java?
- ChartJS. Change axis line color
- set foreground color in FrameLayout in android pro
- Changing background color for a text annotation to
- Keras: Visualize ImageDataGenerator Output
This is how I do it which is easy to remember is to think of RGB as three spokes on a wheel, 120 degrees apart.
The tricky part is saturation, which is to a scale down to the average of those three.
I got this from Brandon Mathis' HSL Picker source code.
It was originally written in CoffeeScript. I converted it to JavaScript using an online converter, and took out the mechanism to verify the user input was a valid RGB value. This answer worked for my usecase, as the most up-voted answer on this post I found to not produce a valid HSL value.
Note that it returns an
hsla
value, witha
representing opacity/transparency.0
is completely transparent, and1
fully opaque.With H, S,and L in [0,1] range:
Java implementation of Mohsen's code
Note that all integer are declared as float (i.e 1f) and must be float, else you will optain grey colors.
HSL to RGB
RGB to HSL
Php Implementation of Chris's C# Code
Also from here, which explains the math of it very well.
This is basically a bunch of functions to convert to and from HSL (Hue Saturation Lightness)
Tested and working on PHP 5.6.15
TL;DR: The full code can be found here on Pastebin.
Hex to HSL
Input: Hex color in format: [#]0f4 or [#]00ff44 (pound sign optional)
Output: HSL in Degrees, Percent, Percent
RGB to HSL
Input: RGB in range 0-255 Output: HSL in Degrees, Percent, Percent.
HSL (0-1 range) to Degrees, Percent, Percent format
For the math calculations, HSL is easier to deal with in the 0-1 range, but for human readability, it's easier in Degrees, Percent, Percent. This function takes HSL in the ranges 0-1, and returns HSL in Degrees, Percent, Percent.
HSL (Degrees, Percent, Percent format) to HSL in range 0-1
This function converts HSL in the format Degrees, Percent, Percent, to the ranges 0-1 for easier computing.
HSL to RGB
Input: HSL in the format Degrees, Percent, Percent Output: RGB in the format
255, 255, 255
.Hue to RGB
This function is called by hslToRgb to convert the hue into the separate RGB values.
HSL to Hex
Input: HSL in format Degrees, Percent, Percent Output: Hex in format
00ff22
(no pound sign).Converts to RGB, then converts separately to hex.
PHP implementation of @Mohsen's code (including Test!)
Sorry to re-post this. But I really haven't seen any other implementation that gives the quality I needed.