HSV toolkit

A toolkit to convert from the RGB into the HSV colour system and vice versa. Also has helper functions for creating an interactive HSV colour picker.

Free to use, I’d just like a copy of the result and please give some credit.

©2006 Marcel Kilgus

HSV_GETH
HSV_GETS
HSV_GETV

Syntax: h = HSV_GETH(r, g, b)
s = HSV_GETS(r, g, b)
v = HSV_GETV(r, g, b)

Returns the respective HSV component of the given RGB colour value. The RGB variables may range from 0 to 255. The resulting h, s, v variables also range from 0 to 255. h might be -1 if it is undefined.

HSV_GETR
HSV_GETG
HSV_GETB

Syntax: r = HSV_GETR(h, s, v)
g = HSV_GETG(h, s, v)
b = HSV_GETB(h, s, v)

Returns the respective RGB component of the given HSV colour value. All variables have the range from 0 to 255.

HSV_GETRGB

Syntax: rgb = HSV_GETRGB(h, s, v)

Returns the complete 24-bit RGB colour for the given HSV components.

HSV_HSSPR

Syntax: adr = HSV_HSSPR

This generates and returns the address of a GD2 sprite that shows all possible hue and saturation colours. The sprite is 128×128 pixels big, horizontal is the hue axis, vertical is the saturation axis. The value component is fixed to 255.

The memory returned by this function can be freed by the HSV_RCHP command after use.

HSV_VSPR

Syntax: adr = HSV_VSPR(x, h, s)

This generates and returns the address of a GD2 sprite that shows all possible value colours for a given pair of hue and saturation components. The sprite is 10×128 pixels big. If x is 0 on entry, new memory is allocated to contain the sprite. If x is not 0, then the given address will be used and returned.

The memory returned by this function can be freed by the HSV_RCHP command after use.

Example:

adr = 0
REPeat
  adr = HSV_VSPR(adr, H%, S%)
  SPRW 0, 0, adr
  [...]
END REPeat
HSV_RCHP adr

HSV_RCHP

Synta: HSV_RCHP adr

Can be used to free the memory that was implicitely allocated by HSV_HSSPR and HSV_VSPR.

Examples

hsvdemo_bas and hsvdemo2_bas are 2 simple SBasic programs that show the use of all commands. Both need the QPtr toolkit to run, but could easily be ported to other PE toolkits within minutes.

In hsvdemo_bas you can use the cursor keys to navigate the H/S sprite and thus change the colour. The value component can be changed by the +/- keys.

In hsvdemo2_bas a click with the mouse within the H/S sprite or the value ramp will change the colour.