CMYK color
#2
A few additional functions and subs for manipulating CMYK color.

Code: (Select All)
Screen _NewImage(640, 480, 32)
Color _RGB32(0, 0, 0), _RGB32(255, 255, 255)
Cls
Print "Just a handful of examples, there are several functions and subs here to"
Print " manipulate and monitor CMYK colors"
Print "mixing colors CMYK(0,70,50,0)  and CMYK(80,0,0,0)"
kl& = mix_cmyk~&(cmyk~&(0, 70, 50, 0), cmyk~&(80, 0, 0, 0))
Line (400, 50)-(450, 100), cmyk~&(0, 70, 50, 0), BF
Line (450, 50)-(500, 100), cmyk~&(80, 0, 0, 0), BF
Line (500, 50)-(600, 100), kl&, BF
Locate 10, 1: Print "Lighten and darken CMYK(50,50,0,0) by 25%"
kl& = cmyk~&(50, 50, 0, 0)
lk& = kl&: dk& = kl&
lighten_cmyk lk&, 25
darken_cmyk dk&, 25
Line (400, 150)-(600, 200), kl&, BF
Line (400, 200)-(500, 250), lk&, BF
Line (500, 200)-(600, 250), dk&, BF
Locate 18, 1: Print "Adding 60 to Yellow channel in each of those."
add_yellow kl&, 60
add_yellow lk&, 60
add_yellow dk&, 60
Line (400, 300)-(600, 350), kl&, BF
Line (400, 350)-(500, 400), lk&, BF
Line (500, 350)-(600, 400), dk&, BF


Function cmyk~& (c As Long, m As Long, y As Long, k As Long)
    ' CMYK process color Cyan, Magenta, Yellow, Black  each  expressed as a percent from 0 to 100
    r = 255 * (100 - c)
    r = (r / 100) * ((100 - k) / 100)
    g = 255 * (100 - m)
    g = (g / 100) * ((100 - k) / 100)
    b = 255 * (100 - y)
    b = (b / 100) * ((100 - k) / 100)
    cmyk~& = _RGB32(r, g, b)
End Function

Function get_c (klr As _Unsigned Long)
    r = _Red32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    get_c = c
End Function
Function get_m (klr As _Unsigned Long)
    g = _Green32(klr)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    get_m = m
End Function
Function get_y (klr As _Unsigned Long)
    b = _Blue32(klr)
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    get_y = y
End Function
Function get_k (klr As _Unsigned Long)
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    If c = m And m = y Then
        k = m
    Else
        k = 0
    End If
    get_k = k
End Function

Sub get_cmyk (klr As _Unsigned Long, c, m, y, k)
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    'the dirty little secret of CMYK in a additive light space is black disappears into CMY  values
    'theres always a CMY vlaue that replicates a CMYK color with no K required but...
    'in print getting rid of K would be a disaster but on a monitor it makes no difference
    If c = m And m = y Then
        k = m
        m = 0
        y = 0
        c = 0
    Else
        k = 0
    End If
End Sub
Sub lighten_cmyk (klr As _Unsigned Long, pp)
    'lightens all four CMYK color channels by the same relative %
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    If c = m And m = y Then
        k = m
        m = 0
        y = 0
        c = 0
    Else
        k = 0
    End If
    c = c * ((100 - pp) / 100)
    m = m * ((100 - pp) / 100)
    y = y * ((100 - pp) / 100)
    k = k * ((100 - pp) / 100)
    klr = cmyk~&(c, m, y, k)
End Sub
Sub darken_cmyk (klr As _Unsigned Long, pp)
    'lightens all four CMYK color channels by the same relative %
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    If c = m And m = y Then
        k = m
        m = 0
        y = 0
        c = 0
    Else
        k = 0
    End If
    c = c * ((100 + pp) / 100): If c > 100 Then c = 100
    m = m * ((100 + pp) / 100): If m > 100 Then m = 100
    y = y * ((100 + pp) / 100): If y > 100 Then y = 100
    k = k * ((100 + pp) / 100): If k > 100 Then k = 100
    klr = cmyk~&(c, m, y, k)
End Sub
Sub add_cyan (klr As _Unsigned Long, cc)
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100) + cc
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    If c = m And m = y Then
        k = m
        m = 0
        y = 0
        c = 0
    Else
        k = 0
    End If
    If c > 100 Then c = 100
    If c < 0 Then c = 0
    klr = cmyk~&(c, m, y, k)
End Sub
Sub add_magenta (klr As _Unsigned Long, mm)
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100) + mm
    y = Int((1 + 1 / 255 - (b / 255)) * 100)
    If c = m And m = y Then
        k = m
        m = 0
        y = 0
        c = 0
    Else
        k = 0
    End If
    If m > 100 Then m = 100
    If m < 0 Then m = 0
    klr = cmyk~&(c, m, y, k)
End Sub
Sub add_yellow (klr As _Unsigned Long, yy)
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100) + yy
    If c = m And m = y Then
        k = m
        m = 0
        y = 0
        c = 0
    Else
        k = 0
    End If
    If y > 100 Then y = 100
    If y < 0 Then y = 0
    klr = cmyk~&(c, m, y, k)
End Sub

Sub add_black (klr As _Unsigned Long, kk)
    r = _Red32(klr)
    g = _Green32(klr)
    b = _Blue32(klr)
    c = Int((1 + 1 / 255 - (r / 255)) * 100)
    m = Int((1 + 1 / 255 - (g / 255)) * 100)
    y = Int((1 + 1 / 255 - (b / 255)) * 100) + yy
    If c = m And m = y Then
        k = m + kk
        m = 0
        y = 0
        c = 0
    Else
        k = 0 + kk
    End If
    If k > 100 Then k = 100
    If k < 0 Then k = 0
    klr = cmyk~&(c, m, y, k)
End Sub
Function mix_cmyk~& (klr1 As _Unsigned Long, klr2 As _Unsigned Long)
    'evenly mix two colors by averaging each channel
    r1 = _Red32(klr1)
    g1 = _Green32(klr1)
    b1 = _Blue32(klr1)
    c1 = Int((1 + 1 / 255 - (r1 / 255)) * 100)
    m1 = Int((1 + 1 / 255 - (g1 / 255)) * 100)
    y1 = Int((1 + 1 / 255 - (b1 / 255)) * 100)
    If c1 = m1 And m1 = y1 Then
        k1 = m1
        m1 = 0
        y1 = 0
        c1 = 0
    Else
        k1 = 0
    End If
    r2 = _Red32(klr2)
    g2 = _Green32(klr2)
    b2 = _Blue32(klr2)
    c2 = Int((1 + 1 / 255 - (r2 / 255)) * 100)
    m2 = Int((1 + 1 / 255 - (g2 / 255)) * 100)
    y2 = Int((1 + 1 / 255 - (b2 / 255)) * 100)
    If c2 = m2 And m2 = y2 Then
        k2 = m2
        m2 = 0
        y2 = 0
        c2 = 0
    Else
        k2 = 0
    End If
    c = Int((c1 + c2) / 2)
    m = Int((m1 + m2) / 2)
    y = Int((y1 + y2) / 2)
    k = Int((k1 + k2) / 2)
    mix_cmyk~& = cmyk~&(c, m, y, k)
End Function
Function add_cmyk~& (klr1 As _Unsigned Long, klr2 As _Unsigned Long)
    'add all the channels in both colors   max value wil be 100 per channel
    r1 = _Red32(klr1)
    g1 = _Green32(klr1)
    b1 = _Blue32(klr1)
    c1 = Int((1 + 1 / 255 - (r1 / 255)) * 100)
    m1 = Int((1 + 1 / 255 - (g1 / 255)) * 100)
    y1 = Int((1 + 1 / 255 - (b1 / 255)) * 100)
    If c1 = m1 And m1 = y1 Then
        k1 = m1
        m1 = 0
        y1 = 0
        c1 = 0
    Else
        k1 = 0
    End If
    r2 = _Red32(klr2)
    g2 = _Green32(klr2)
    b2 = _Blue32(klr2)
    c2 = Int((1 + 1 / 255 - (r2 / 255)) * 100)
    m2 = Int((1 + 1 / 255 - (g2 / 255)) * 100)
    y2 = Int((1 + 1 / 255 - (b2 / 255)) * 100)
    If c2 = m2 And m2 = y2 Then
        k2 = m2
        m2 = 0
        y2 = 0
        c2 = 0
    Else
        k2 = 0
    End If
    c = c1 + c2: If c > 100 Then c = 100
    m = m1 + m2: If m > 100 Then m = 100
    y = y1 + y2: If y > 100 Then y = 100
    k = k1 + k2: If k > 100 Then k = 100
    add_cmyk~& = cmyk~&(c, m, y, k)
End Function
Reply


Messages In This Thread
CMYK color - by James D Jarvis - 06-29-2022, 03:35 AM
RE: CMYK color - by James D Jarvis - 06-30-2022, 01:24 PM



Users browsing this thread: 1 Guest(s)