Conversion from sRGB to simple CMYK:
(RGB input assumed to be in range 0...255)
(CMYK output will be in range 0...255)
K := 255 - max(R, G, B)
K := K - K * K / 510
(The above line is optional; it causes less K and more CMY to be used for dark colors. Any adjustment will work here as long as it never increases the value of K.)
C := 255 - R / (1 - K / 255)
M := 255 - G / (1 - K / 255)
Y := 255 - B / (1 - K / 255)
The following naive (but common) formula will convert back to the original RGB values exactly (subject to roundoff error):
R := (255 - C) * (255 - K) / 255
G := (255 - M) * (255 - K) / 255
B := (255 - Y) * (255 - K) / 255
More-correct conversions between RGB and CMYK are a bit more complicated, and generally device-dependent.