Skip to content

Commit b2d5c62

Browse files
Update README.md
1 parent f5600b6 commit b2d5c62

File tree

1 file changed

+191
-2
lines changed

1 file changed

+191
-2
lines changed

README.md

Lines changed: 191 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ You can also convert between `Color`, `HSL`, `HSV`, `RGB`, and
297297
colors with utility functions. call color model you want to conveert from, `to` then the
298298
color model you want to convert to
299299

300-
From Color
300+
### Color Conversion
301301

302302
```kotlin
303303

@@ -560,4 +560,193 @@ ColorInt-RGB Conversions
560560
argbIn[2] = green
561561
argbIn[3] = blue
562562
}
563-
```
563+
```
564+
565+
566+
### HSL Conversion
567+
```kotlin
568+
569+
570+
/**
571+
* Convert HSL components(hue-saturation-lightness) to HSV
572+
* (hue-saturation-value) components.
573+
* @param hue in [0..360f]
574+
* @param saturation in [0..1f]
575+
* @param lightness in [0..1f]
576+
*/
577+
fun hslToHSV(
578+
hue: Float,
579+
saturation: Float,
580+
lightness: Float
581+
): FloatArray {
582+
val value = lightness + saturation * lightness.coerceAtMost(1 - lightness)
583+
val saturationHSV = if (value == 0f) 0f else 2 * (1 - lightness / value)
584+
return floatArrayOf(hue, saturationHSV.coerceIn(0f, 1f), value.coerceIn(0f, 1f))
585+
}
586+
587+
/**
588+
* Convert HSL components(hue-saturation-lightness) to HSV
589+
* (hue-saturation-value) components.
590+
*
591+
* ```
592+
* hsv[0] is Hue [0 .. 360)
593+
* hsv[1] is Saturation [0...1]
594+
* hsv[2] is Value [0...1]
595+
* ```
596+
* ```
597+
* hsl[0] is Hue [0 .. 360)
598+
* hsl[1] is Saturation [0...1]
599+
* hsl[2] is Lightness [0...1]
600+
* ```
601+
*/
602+
fun hslToHSV(hslIn: FloatArray): FloatArray {
603+
return hslToHSV(hslIn[0], hslIn[1], hslIn[2])
604+
}
605+
606+
/*
607+
HSL-ColorInt Conversions
608+
*/
609+
610+
/**
611+
* Convert HSL (hue-saturation-lightness) components to a RGB color in [Integer] format.
612+
*
613+
* For instance, red =255, green =0, blue=0 is -65536
614+
* ```
615+
* hsl[0] is Hue [0 .. 360)
616+
* hsl[1] is Saturation [0...1]
617+
* hsl[2] is Lightness [0...1]
618+
* ```
619+
*/
620+
fun hslToColorInt(hslIn: FloatArray): Int {
621+
return hslToColorInt(hslIn[0], hslIn[1], hslIn[2])
622+
}
623+
624+
/**
625+
* Convert HSL (hue-saturation-lightness) components to a RGB color in [Integer] format.
626+
* @param hue in [0..360f]
627+
* @param saturation in [0..1f]
628+
* @param lightness in [0..1f]
629+
*/
630+
fun hslToColorInt(
631+
hue: Float,
632+
saturation: Float,
633+
lightness: Float
634+
): Int {
635+
return ColorUtils.HSLToColor(floatArrayOf(hue, saturation, lightness))
636+
}
637+
638+
639+
/*
640+
HSL-RGB Conversions
641+
*/
642+
643+
/**
644+
* Convert HSL (hue-saturation-lightness) components to a RGB red, green blue array.
645+
* ```
646+
* hsl[0] is Hue [0 .. 360)
647+
* hsl[1] is Saturation [0...1]
648+
* hsl[2] is Lightness [0...1]
649+
* ```
650+
* ```
651+
* rgb[0] is Red [0 .. 255]
652+
* rgb[1] is Green [0...255]
653+
* rgb[2] is Blue [0...255]
654+
* ```
655+
* @param hslIn 3 element array which holds the input HSL components.
656+
*/
657+
fun hslToRGB(hslIn: FloatArray): IntArray {
658+
return colorIntToRGBArray(hslToColorInt(hslIn))
659+
}
660+
661+
/**
662+
* Convert HSL (hue-saturation-lightness) components to a RGB red, green blue array.
663+
* ```
664+
* Hue is [0 .. 360)
665+
* Saturation is [0...1]
666+
* Lightness is [0...1]
667+
* ```
668+
*
669+
* ```
670+
* rgb[0] is Red [0 .. 255]
671+
* rgb[1] is Green [0...255]
672+
* rgb[2] is Blue [0...255]
673+
* ```
674+
*/
675+
fun hslToRGB(
676+
hue: Float,
677+
saturation: Float,
678+
lightness: Float
679+
): IntArray {
680+
return colorIntToRGBArray(
681+
color = hslToColorInt(hue, saturation, lightness)
682+
)
683+
}
684+
685+
/**
686+
* Convert HSL (hue-saturation-lightness) components to a RGB red, green blue array.
687+
* ```
688+
* rgb[0] is Red [0 .. 255]
689+
* rgb[1] is Green [0...255]
690+
* rgb[2] is Blue [0...255]
691+
* ```
692+
* @param hue in [0..360f]
693+
* @param saturation in [0..1f]
694+
* @param lightness in [0..1f]
695+
* @param rgbIn 3 element array which holds the input RGB components.
696+
*/
697+
fun hslToRGB(
698+
hue: Float,
699+
saturation: Float,
700+
lightness: Float,
701+
rgbIn: IntArray
702+
) {
703+
colorIntToRGBArray(hslToColorInt(hue, saturation, lightness), rgbIn)
704+
}
705+
706+
707+
/**
708+
* Convert HSL (hue-saturation-lightness) to RGB red, green, blue components in [0f..1f] range.
709+
* ```
710+
* rgb[0] is Red [0f .. 1f)
711+
* rgb[1] is Green [0f...1f]
712+
* rgb[2] is Blue [0f...1f]
713+
* ```
714+
* @param hue in [0..360f]
715+
* @param saturation in [0..1f]
716+
* @param lightness in [0..1f]
717+
*
718+
* @return 3 element array which holds the output RGB components.
719+
*/
720+
fun hslToRGBFloat(
721+
hue: Float,
722+
saturation: Float,
723+
lightness: Float
724+
): FloatArray {
725+
val color = Color.hsl(hue, saturation, lightness)
726+
return floatArrayOf(color.red, color.green, color.blue)
727+
}
728+
729+
/**
730+
* Convert HSL (hue-saturation-lightness) to RGB red, green, blue components in [0f..1f] range.
731+
* ```
732+
* rgb[0] is Red [0f .. 1f)
733+
* rgb[1] is Green [0f...1f]
734+
* rgb[2] is Blue [0f...1f]
735+
* ```
736+
* @param hue in [0..360f]
737+
* @param saturation in [0..1f]
738+
* @param lightness in [0..1f]
739+
* @param rgbIn 3 element array which holds the output RGB components.
740+
*/
741+
fun hslToRGBFloat(
742+
hue: Float,
743+
saturation: Float,
744+
lightness: Float,
745+
rgbIn: FloatArray
746+
) {
747+
val color = Color.hsl(hue, saturation, lightness)
748+
rgbIn[0] = color.red
749+
rgbIn[1] = color.green
750+
rgbIn[2] = color.blue
751+
}
752+
```

0 commit comments

Comments
 (0)