// Copyright (c) 2017, Marty Mills <daggerbot@gmail.com>
// This software is available under the terms of the zlib license.
// See COPYING.md for more information.

use std::ops::{Add, Div, Mul, Sub};

use lum::Lum;
use rgb::Rgb;

macro_rules! impl_color_scalar_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $color:ident($($field:ident),+);)* } => { $(
        impl<T> $op_trait<T> for $color<T>
            where T: Copy + $op_trait<Output = T>
        {
            type Output = $color<T>;

            fn $op_fn (self, rhs: T) -> $color<T> {
                $color { $($field: self.$field.$op_fn(rhs)),* }
            }
        }
    )* };
}

macro_rules! impl_color_color_ops {
    { $(impl $op_trait:ident::$op_fn:ident for $color:ident($($field:ident),+);)* } => { $(
        impl<T> $op_trait for $color<T>
            where T: $op_trait<Output = T>
        {
            type Output = $color<T>;

            fn $op_fn (self, rhs: $color<T>) -> $color<T> {
                $color { $($field: self.$field.$op_fn(rhs.$field)),* }
            }
        }
    )* };
}

impl_color_scalar_ops! {
    impl Mul::mul for Lum(l);
    impl Div::div for Lum(l);
    impl Mul::mul for Rgb(r, g, b);
    impl Div::div for Rgb(r, g, b);
}

impl_color_color_ops! {
    impl Add::add for Lum(l);
    impl Sub::sub for Lum(l);
    impl Mul::mul for Lum(l);
    impl Div::div for Lum(l);
    impl Add::add for Rgb(r, g, b);
    impl Sub::sub for Rgb(r, g, b);
    impl Mul::mul for Rgb(r, g, b);
    impl Div::div for Rgb(r, g, b);
}
