SATURATION32: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:


{{PageDescription}}
{{PageDescription}}
* The value returned is of type [[DOUBLE]] in the range 0 to 100 percent, use [[CINT]] to work with integers only.
* The value returned is of type [[DOUBLE]] in the range 0 to 100 percent.
** 100% is the richest color possible, as closer the value comes to 0%, as more faded is the color, ending at dull gray.
** 100% is the richest color possible, as closer the value comes to 0% as more faded is the color, ending at dull gray.
** The intensity of the gray (i.e. black >> darkgray >> midgray >> lightgray >> white) depends on the brightness value.
** The intensity of the gray (i.e. black >> darkgray >> midgray >> lightgray >> white) depends on the brightness value.
;Naming differences
;Naming differences
Line 20: Line 20:
:* However, this function is not suitable for the HSL (L=Lightness) colorspace, which is widely used in the Web/CSS.
:* However, this function is not suitable for the HSL (L=Lightness) colorspace, which is widely used in the Web/CSS.
;Precision drawbacks
;Precision drawbacks
:* When converting between colorspaces, rounding errors can occur. While the HSB colorspace has virtually infinite precision only limited by the used floating point type, RGB is represented in 8-bit integers (0-255) only, which can lead to quantization errors. In fact, not all colors in HSB can be accurately represented in RGB and vice versa.
:* When converting between colorspaces, rounding errors can occur.
:* However, if you limit yourself to only passing integer values to [[_HSB32]] or [[_HSBA32]], just as you do for the respective RGB colorspace functions [[_RGB32]] or [[_RGBA32]], then you can get back that exact values by [[CINT]]ing the results returned by [[_HUE32]], [[_SATURATION32]] and [[_BRIGHTNESS32]].
:* While the HSB colorspace has virtually infinite precision using floating point values, RGB is limited to 8-bit integers (0-255) only, which can lead to quantization errors.
:* In fact, not all colors in HSB can be accurately represented in RGB, but the opposite is possible.
:* It can be guaranteed, that the [[_HUE32]], [[_SATURATION32]] and [[_BRIGHTNESS32]] values retrieved from any arbitrary RGB color will reproduce the exact same RGB color when passed back through [[_HSB32]].




Line 38: Line 40:


{{PageExamples}}
{{PageExamples}}
;Example 1: Showcasing the HSB value retrieving functions and demonstrate the quantization errors.
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Text|<nowiki>"Creating a color using the HSB colorspace..."</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Creating a color using the HSB colorspace..."</nowiki>|#FFB100}}
c~& = {{Cl|_HSB32}}({{Text|90|#F580B1}}, {{Text|75|#F580B1}}, {{Text|65|#F580B1}})
cOrig~& = {{Cl|_HSB32}}({{Text|90|#F580B1}}, {{Text|75|#F580B1}}, {{Text|65|#F580B1}})
 
{{Cl|PRINT}} {{Text|<nowiki>"_HSB32( 90, 75, 65 ) = _RGB32("</nowiki>|#FFB100}}; {{Cl|_RED32}}(cOrig~&); {{Text|<nowiki>","</nowiki>|#FFB100}}; {{Cl|_GREEN32}}(cOrig~&); {{Text|<nowiki>","</nowiki>|#FFB100}}; {{Cl|_BLUE32}}(cOrig~&); {{Text|<nowiki>")"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"_HSB32( 90, 75, 65 ) = _RGB32("</nowiki>|#FFB100}}; {{Cl|_RED32}}(c~&); {{Text|<nowiki>","</nowiki>|#FFB100}}; {{Cl|_GREEN32}}(c~&); {{Text|<nowiki>","</nowiki>|#FFB100}}; {{Cl|_BLUE32}}(c~&); {{Text|<nowiki>")"</nowiki>|#FFB100}}
{{Cl|PRINT}}
{{Cl|PRINT}} {{Text|<nowiki>"Back to HSB values (notice the quantization errors)."</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"The HSB color was not accurately representable in RGB."</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Hue.......:"</nowiki>|#FFB100}}; {{Cl|_HUE32}}(cOrig~&)
{{Cl|PRINT}} {{Text|<nowiki>"Saturation:"</nowiki>|#FFB100}}; {{Cl|_SATURATION32}}(cOrig~&)
{{Cl|PRINT}} {{Text|<nowiki>"Brightness:"</nowiki>|#FFB100}}; {{Cl|_BRIGHTNESS32}}(cOrig~&)
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}} {{Text|<nowiki>"back to HSB values (notice the precision loss)..."</nowiki>|#FFB100}}
cBack~& = {{Cl|_HSB32}}({{Cl|_HUE32}}(cOrig~&), {{Cl|_SATURATION32}}(cOrig~&), {{Cl|_BRIGHTNESS32}}(cOrig~&))
{{Cl|PRINT}} {{Text|<nowiki>"Hue.......:"</nowiki>|#FFB100}}; {{Cl|_HUE32}}(c~&)
{{Cl|PRINT}} {{Text|<nowiki>"However, when using the retrieved HSB values again for conversion,"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Saturation:"</nowiki>|#FFB100}}; {{Cl|_SATURATION32}}(c~&)
{{Cl|PRINT}} {{Text|<nowiki>"then we definitly get back the exact same RGB color again."</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Brightness:"</nowiki>|#FFB100}}; {{Cl|_BRIGHTNESS32}}(c~&)
{{Cl|PRINT}} {{Text|<nowiki>"Red.......:"</nowiki>|#FFB100}}; {{Cl|_RED32}}(cBack~&)
{{Cl|PRINT}} {{Text|<nowiki>"Green.....:"</nowiki>|#FFB100}}; {{Cl|_GREEN32}}(cBack~&)
{{Cl|PRINT}} {{Text|<nowiki>"Blue......:"</nowiki>|#FFB100}}; {{Cl|_BLUE32}}(cBack~&)
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}} {{Text|<nowiki>"but with CINT() we can get back the original values..."</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Conclusion:"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Hue.......:"</nowiki>|#FFB100}}; {{Cl|CINT}}({{Cl|_HUE32}}(c~&))
{{Cl|PRINT}} {{Text|<nowiki>"Not every HSB color can be accurately represented in 8-bit integer RGB,"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Saturation:"</nowiki>|#FFB100}}; {{Cl|CINT}}({{Cl|_SATURATION32}}(c~&))
{{Cl|PRINT}} {{Text|<nowiki>"but every RGB color can be accurately represented in floating point HSB."</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Text|<nowiki>"Brightness:"</nowiki>|#FFB100}}; {{Cl|CINT}}({{Cl|_BRIGHTNESS32}}(c~&))
 
{{Cl|END}}
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}

Latest revision as of 19:47, 30 January 2025

The _SATURATION32 function returns the saturation value (HSB colorspace) of a given 32-bit ARGB color.


Syntax

saturation# = _SATURATION32(argbColor~&)


Parameters

  • argbColor~& is the 32-bit ARGB color value to retrieve the saturation value from.


Description

  • The value returned is of type DOUBLE in the range 0 to 100 percent.
    • 100% is the richest color possible, as closer the value comes to 0% as more faded is the color, ending at dull gray.
    • The intensity of the gray (i.e. black >> darkgray >> midgray >> lightgray >> white) depends on the brightness value.
Naming differences
  • The HSB (B=Brightness) colorspace is also known as HSV (V=Value) and sometimes even called HSI (I=Intensity), but that's all just different names for the same thing.
  • However, this function is not suitable for the HSL (L=Lightness) colorspace, which is widely used in the Web/CSS.
Precision drawbacks
  • When converting between colorspaces, rounding errors can occur.
  • While the HSB colorspace has virtually infinite precision using floating point values, RGB is limited to 8-bit integers (0-255) only, which can lead to quantization errors.
  • In fact, not all colors in HSB can be accurately represented in RGB, but the opposite is possible.
  • It can be guaranteed, that the _HUE32, _SATURATION32 and _BRIGHTNESS32 values retrieved from any arbitrary RGB color will reproduce the exact same RGB color when passed back through _HSB32.


Availability


Examples

Example 1
Showcasing the HSB value retrieving functions and demonstrate the quantization errors.
PRINT "Creating a color using the HSB colorspace..."
cOrig~& = _HSB32(90, 75, 65)
PRINT "_HSB32( 90, 75, 65 ) = _RGB32("; _RED32(cOrig~&); ","; _GREEN32(cOrig~&); ","; _BLUE32(cOrig~&); ")"
PRINT
PRINT "Back to HSB values (notice the quantization errors)."
PRINT "The HSB color was not accurately representable in RGB."
PRINT "Hue.......:"; _HUE32(cOrig~&)
PRINT "Saturation:"; _SATURATION32(cOrig~&)
PRINT "Brightness:"; _BRIGHTNESS32(cOrig~&)
PRINT
cBack~& = _HSB32(_HUE32(cOrig~&), _SATURATION32(cOrig~&), _BRIGHTNESS32(cOrig~&))
PRINT "However, when using the retrieved HSB values again for conversion,"
PRINT "then we definitly get back the exact same RGB color again."
PRINT "Red.......:"; _RED32(cBack~&)
PRINT "Green.....:"; _GREEN32(cBack~&)
PRINT "Blue......:"; _BLUE32(cBack~&)
PRINT
PRINT "Conclusion:"
PRINT "Not every HSB color can be accurately represented in 8-bit integer RGB,"
PRINT "but every RGB color can be accurately represented in floating point HSB."
END


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link