Funktionen in Nim, um Farbwerte zwischen RGB und BGR und zwischen RGB und HSV umzuwandeln konnte ich nicht finden, also habe ich das mal selbst geschrieben.
Ich habe die Funktionen überladen, sodass man sowohl einen int32 Wert übergeben kann, als auch drei Einzelwerte für RGB bzw. HSV. Außerdem gibt es noch jeweils ein Objekt für die Beiden.
Bei RGB:
Die Werte für R, G und B liegen jeweils zwischen 0...255 (0x00...0xFF).
Bei HSV:
Der Wert für H liegt zwischen 0...359. Und die Werte für S und V zwischen 0.00...100.00
Das Modul:
Code
import math
type
RGB* = object
r*, g*, b*: uint8
HSV* = object
h*, s*, v*: float
# Farbwerte von Red, Green, Blue (RGB) nach int32 konvertieren
proc toInt32*(r, g, b: uint8): int32 =
result = (int32(r) shl 16) or (int32(g) shl 8) or int32(b)
# toInt32 mit RGB-Object (Funktion ueberladen)
proc toInt32*(rgb: RGB): int32 =
result = toInt32(rgb.r, rgb.g, rgb.b)
# Farbwert von Red, Green, Blue (RGB) nach Blue, Green, Red (BGR) und umgekehrt
proc switchColor*(c: int32): int32 =
result = ((c and 0x00FF0000) shr 16) or (c and 0x0000FF00) or ((c and 0x000000FF) shl 16)
# switchColor mit drei Einzelwerten (Funktion ueberladen)
proc switchColor*(r, g, b: uint8): int32 =
result = switchColor(toInt32(r, g, b))
# Minimumwert ermitteln aus mehreren Floats (mit Maxwert von 1.0)
# Nur fuer modulinterne Aufgaben!
proc myMin(f: varargs[float]): float =
result = 1.0
for v in f:
if v < result: result = v
# Maximumwert ermitteln aus mehreren Floats (mit Minwert von 0.0)
# Nur fuer modulinterne Aufgaben!
proc myMax(f: varargs[float]): float =
result = 0.0
for v in f:
if v > result: result = v
# Floatwert auf zwei Nachkommastellen beschraenken
# Nur fuer modulinterne Aufgaben!
proc myRound(x: float): float =
result = floor(x * 100) / 100
# Farbwert von Red, Green, Blue (RGB) nach Hue, Saturation, Value (HSV)
# Das Ergebnis (HSV-Object) sind drei Floatwerte (mit max. zwei Nachkommastellen).
proc RGBtoHSV*(c: int32): HSV =
var r, g, b, minV, maxV, del, h: float
r = ((c and 0x00FF0000) shr 16).toFloat / 255
g = ((c and 0x0000FF00) shr 8).toFloat / 255
b = ((c and 0x000000FF)).toFloat / 255
minV = myMin(r, g, b)
maxV = myMax(r, g, b)
del = maxV - minV
result.v = myRound(maxV * 100)
if del == 0:
result.h = 0
result.s = 0
else:
result.s = myRound((del / maxV) * 100)
if maxV == r: h = 60 * (0 + (g - b) / del)
if maxV == g: h = 60 * (2 + (b - r) / del)
if maxV == b: h = 60 * (4 + (r - g) / del)
if h < 0: h += 360
result.h = myRound(h)
# RGBtoHSV mit drei Einzelwerten (Funktion ueberladen)
proc RGBtoHSV*(r, g, b: uint8): HSV =
result = RGBtoHSV(toInt32(r, g, b))
# RGBtoHSV mit RGB-Object (Funktion ueberladen)
proc RGBtoHSV*(rgb: RGB): HSV =
result = RGBtoHSV(toInt32(rgb))
# Farbwert von Hue, Saturation, Value (HSV) nach Red, Green, Blue (RGB)
# Das Ergebnis (RGB) wird als int32 ausgegeben
proc HSVtoRGB*(hsv: HSV): int32 =
var
h, s, v, f: float
iH, iV, iP, iQ, iT: uint8
res: RGB
h = hsv.h
s = hsv.s / 100
v = hsv.v / 100
f = h / 60 - floor(h / 60)
v *= 255
iH = uint8(floor(h / 60) mod 6)
iV = uint8(v)
iP = uint8(v * (1 - s))
iQ = uint8(v * (1 - f * s))
iT = uint8(v * (1 - (1 - f) * s))
case iH
of 0: res = RGB(r: iV, g: iT, b: iP)
of 1: res = RGB(r: iQ, g: iV, b: iP)
of 2: res = RGB(r: iP, g: iV, b: iT)
of 3: res = RGB(r: iP, g: iQ, b: iV)
of 4: res = RGB(r: iT, g: iP, b: iV)
else: res = RGB(r: iV, g: iP, b: iQ)
result = res.toInt32
# HSVtoRGB mit drei Einzelwerten (Funktion ueberladen)
proc HSVtoRGB*(h, s, v: float): int32 =
result = HSVtoRGB(HSV(h: h, s: s, v: v))
Alles anzeigen
Und das Beispiel:
Python
import ColorConvert
from strutils import toHex
let
r: uint8 = 0xAA
g: uint8 = 0xBB
b: uint8 = 0xCC
let bgrColor = toInt32(b, g, r) # die drei Einzelwerte nach int32 konvertieren
echo "Color (BGR): 0x", bgrColor.toHex
let rgbColor = bgrColor.switchColor # switch BGR <-> RGB
echo "Color (RGB): 0x", rgbColor.toHex
let hsvColor = RGBtoHSV(rgbColor) # Uebergabe als int32
echo "Color (HSV): H = ", hsvColor.h, ", S = ", hsvColor.s, ", V = ", hsvColor.v
let hsvColor2 = RGBtoHSV(0xAA, 0xBB, 0xCC) # Uebergabe mit drei Einzelwerten
echo "Color (HSV): H = ", hsvColor2.h, ", S = ", hsvColor2.s, ", V = ", hsvColor2.v
let rgbColor2 = HSVtoRGB(hsvColor) # Uebergabe mit HSV-Object
echo "Color (RGB): 0x", rgbColor2.toHex
let rgbColor3 = HSVtoRGB(210, 16.66, 80) # Uebergabe mit drei Einzelwerten
echo "Color (RGB): 0x", rgbColor3.toHex
Alles anzeigen
Alles auch zum downloaden im Anhang.