2
\$\begingroup\$
let deltaF: TransformBlock = { f in
        let transformation = (f > pow((6.0/29.0), 3.0)) ? pow(f, 1.0/3.0) : (1/3) * pow((29.0/6.0), 2.0) * f + 4/29.0

        return (transformation)
    }

Says here that this expression is too complex to be solved in reasonable time. Is there any way to simplify it?

This code is meant to convert RGB colors into CIELAB colors. I've done some research online and couldn't find an updated version of that code.

func CIE_LAB() -> (l: CGFloat, a: CGFloat, b: CGFloat, alpha: CGFloat) {
    // Get XYZ
    let xyzT = xyz()
    let x = xyzT.x/95.047
    let y = xyzT.y/100.000
    let z = xyzT.z/108.883

    // Transfrom XYZ to L*a*b
    let deltaF: TransformBlock = { f in
        let transformation = (f > pow((6.0/29.0), 3.0)) ? pow(f, 1.0/3.0) : (1/3) * pow((29.0/6.0), 2.0) * f + 4/29.0

        return (transformation)
    }
    let X = deltaF(x)
    let Y = deltaF(y)
    let Z = deltaF(z)
    let L = 116*Y - 16
    let a = 500 * (X - Y)
    let b = 200 * (Y - Z)

    return (L, a, b, xyzT.alpha)
}
\$\endgroup\$
1
\$\begingroup\$

The main thing that would speed it up is to not constantly calculate constant values. For example, you can replace:

pow ((6.0 / 29.0), 3.0)

with

0.00885645167904

Preferably you'll give it a name like:

let threshold = 0.00885645167904

Likewise for the other call to pow(). pow() is a very expensive function to compute, so removing it can be a big win.

\$\endgroup\$
  • \$\begingroup\$ Hey thanks! That worked. Apparently swift doesn't like expression with too much pow(number, power) \$\endgroup\$ – Ler Ws Oct 18 '16 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.