Search code examples
swiftsprite-kitrgbuicolorskshapenode

UIColor(r: g: b:) not working properly


I have a method called drawLine(), that draws a line between two points (obviously). It has a start point and an end point that are constantly being moved around, so everytime the update() method is called, the line is deleted, and drawLine() is called again, creating a new line.

func drawLine(firstPoint: CGPoint, secondPoint: CGPoint) -> SKShapeNode
{
    var path = CGPathCreateMutable()
    CGPathMoveToPoint(path, nil, firstPoint.x, firstPoint.y)
    CGPathAddLineToPoint(path, nil, secondPoint.x, secondPoint.y)
    let shape = SKShapeNode()
    shape.path = path
    let strength = sqrt(pow((firstPoint.x - secondPoint.x), 2) + pow((firstPoint.y - secondPoint.y), 2))
    NSLog("\(strength)")
    shape.strokeColor = UIColor(red: strength/3, green: 255 - strength/3, blue: 0, alpha: 1)
    shape.lineWidth = 2
    return shape
}

The line stretches from "firstPoint" to "secondPoint" without any problems, but the rgb value, which is calculated using a variable called "strength" that is proportional to the length of the line, doesn't seem to be working properly. The line is always yellow no matter what, until the "strength" reaches a value of 765 (which also happens to be 255 * 3) which is when it abruptly switches to red. Why aren't I getting a gradual change? Also I tried inputting the values for turquoise (r: 50, g: 214, b: 200) but I only got grey. Why is this? Thanks in advance (:


Solution

  • The RGB values range from 0.0 to 1.0, not 0 to 255.