Respond to this messageReturn to Index
Original Message
  • HSV and HSL Colour Space
    • bluebear (no login)
      Posted May 24, 2010 12:17 PM

      I looked up into this subject today, and wrote a program to demonstrate the HSV and HSL colour space. It seems to be working, (but im no expert on the subject so it might have some bug in it). I thought I'd share it with you because HSV and HSL are VERY useful to work with colours of nearly anything. If you have anything to do with colored graphics then I'd surely recommend looking up about this subject. By the way, the conversion algorithm was easier to code then I thought, I started with this diagram:
      http://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/HSV-RGB-comparison.svg/451px-HSV-RGB-comparison.svg.png

      Hue is the teint (red, green, blue, purple yellow etc.)
      Value is the brightness of the colour, 0 being black, 100 being the full colour (Value is exclusive to HSV and is replaced by lightness in HSL)
      Saturation how pronounced the colour is, 0 means a grey colour, 100 a full pronounced colour
      Lightness
      Lightness simulates the amount of light that falls on a colour. 0 means that the colour becomes black, 100 means that the colour becomes white. So at 50, the colour is percieved neutrally.

      This may not be really clear, but if you look up in the subject (no idea if that is a valid expression btw) you find out soon enough. HSL is the best option to use for lightning.

      Anyway, the code:

      CLS
      SCREEN 13
      DEFINT A-Z
      OUT &H3C8, 1: OUT &H3C9, 63: OUT &H3C9, 63: OUT &H3C9, 63
      COLOR 1
      FOR c = 2 TO 255
        LINE (c, 80)-(c, 120), c
      NEXT
      Value = 50
      sat = 50
      mode = 1
      LOCATE 5, 1
      PRINT "Press 1 for HSL and 2 for HSV"
      DO
        LOCATE 1, 1
        IF mode = 1 THEN
        PRINT "1. HSL Colour Space"
        PRINT "Hue - Saturation - Lightness"
        PRINT "Lightness:" + STR$(Value) + "  "
        ELSEIF mode = 2 THEN
        PRINT "2. HSV Colour Space"
        PRINT "Hue - Saturation - Value    "
        PRINT "Value:" + STR$(Value) + "       "
        END IF
        PRINT "Saturation:" + STR$(sat) + "  "
        FOR c = 2 TO 255
          h = (c - 2) / 253 * 359
          a = h \ 60
          r = 60 * -(((a = 0) OR (a = 5))) + (119 - h) * -(a = 1) + (h - 239) * -(a = 4)
          g = h * -(a = 0) + 60 * -((a = 1) OR (a = 2)) + (239 - h) * -(a = 3)
          b = 60 * -((a = 3) OR (a = 4)) + (h - 119) * -(a = 2) + (359 - h) * -(a = 5)
          r = r * 63 \ 60
          g = g * 63 \ 60
          b = b * 63 \ 60
          IF mode = 2 THEN
            r = r * Value \ 100
            g = g * Value \ 100
            b = b * Value \ 100
          END IF
          IF r > 63 THEN r = 63
          IF r < 0 THEN r = 0
          IF g > 63 THEN g = 63
          IF g < 0 THEN g = 0
          IF b > 63 THEN b = 63
          IF b < 0 THEN b = 0
          v = Value / 100 * 63
          r = (r * sat + v * (100 - sat)) \ 100
          g = (g * sat + v * (100 - sat)) \ 100
          b = (b * sat + v * (100 - sat)) \ 100
          IF mode = 1 THEN
            IF Value > 50 THEN
              r = (r * (100 - Value) + 63 * (Value - 50)) \ 50
              g = (g * (100 - Value) + 63 * (Value - 50)) \ 50
              b = (b * (100 - Value) + 63 * (Value - 50)) \ 50
            ELSEIF Value < 50 THEN
              r = r * Value \ 50
              g = g * Value \ 50
              b = b * Value \ 50
            END IF
          ELSEIF mode = 2 THEN
            r = (r * sat + v * (100 - sat)) \ 100
            g = (g * sat + v * (100 - sat)) \ 100
            b = (b * sat + v * (100 - sat)) \ 100
          END IF
          OUT &H3C8, c
          OUT &H3C9, r
          OUT &H3C9, g
          OUT &H3C9, b
        NEXT
        key$ = INKEY$
        IF key$ = CHR$(0) + "H" THEN IF Value < 100 THEN Value = Value + 1
        IF key$ = CHR$(0) + "K" THEN IF sat > 0 THEN sat = sat - 1
        IF key$ = CHR$(0) + "M" THEN IF sat < 100 THEN sat = sat + 1
        IF key$ = CHR$(0) + "P" THEN IF Value > 0 THEN Value = Value - 1
        IF key$ = "1" THEN mode = 1
        IF key$ = "2" THEN mode = 2
        IF key$ = CHR$(27) OR UCASE$(key$) = "Q" THEN EXIT DO
      LOOP
      END
    Your Name
    Message Title
    Message Text
    Options