Reputation: 53
I wrote a simple code to convert rgb to cmyk. Then I defined the datatype Color
.
now I got this error:
Couldn't match expectet type `Color' with actual type `(t0, t1, t2)'
I thought Color
would refer to Rgb(Int,Int,Int)
. What I have done wrong?
My code:
data Color = Rgb (Int,Int,Int) | Cmyk Double Double Double Double deriving (Show)
rgb2cmyk :: Color -> Color
rgb2cmyk (Rgb (0,0,0)) = (Cmyk 0 0 0 1)
rgb2cmyk (Rgb (r,g,b)) = (Cmyk c m y k)
where
rd = (fromIntegral r)/255
gd = (fromIntegral g)/255
bd = (fromIntegral b)/255
w = max3 rd gd bd
c = w - rd/w
m = w - gd/w
y = w - bd/w
k = 1 - w
max3 :: Double -> Double -> Double -> Double
max3 a b c | a>=b && a>=c = a
| b>=a && b>=c = b
| otherwise = c
test1 = rgb2cmyk 233 123 123
Error occours in test1
line. Do I have to write rgb2cmyk $ Rgb
?
Upvotes: 2
Views: 106
Reputation: 42786
I would suggest to keep the syntax, keep Rgb Int Int Int
instead of Rgb (Int,Int,Int
)
data Color = Rgb Int Int Int | Cmyk Double Double Double Double deriving (Show)
rgb2cmyk :: Color -> Color
rgb2cmyk (Rgb 0 0 0) = (Cmyk 0 0 0 1)
rgb2cmyk (Rgb r g b) = (Cmyk c m y k)
where
(...)
*Main> rgb2cmyk $ Rgb 0 0 0
Cmyk 0.0 0.0 0.0 1.0
Explaining the comment:
You cant call rgb2cmyk
as rgb2cmyk x y z
, the function is expecting an Rgb x y z
so you must call it as rgb2cmyk Rgb 10 10 10
for example. Rgb
is a Color
is a constructor and either Rgb
or Cmyk
must be called if your function takes a Color
.
Upvotes: 2