colors - How to prevent a failable initializer of a Swift enum from returning nil within a range of raw numbers? -


i implemented enum represent 256 colors of color map this:

enum mapcolor:uint8 {     case black = 0     case white = 1     case red = 2     case lightred = 3     case orange = 7     ...     case rampblue = 78     ...     case rampred = 255 } 

however, have not given case each color in color map. added specific cases important colors want specify name, green, orange, red, yellow, gray, lightyellow, etc. allows me specify color this:

let color = mapcolor.orange 

without worrying underlying raw value. @ same time allow me have 256 colors used in gradients in game. why between 78 , 255 specified in color map gradient of colors goes blue (rampblue) , ends in red (rampred).

however, wanted implement random color generator returned raw value between 0 , 255, possible color gamut color map, this:

func fillcolor() -> mapcolor {     return mapcolor(rawvalue: uint8(abs(utilities.random(maximum: 255))))! } 

however, obviously, causes segmentation fault whenever tries initialize mapcolor object raw value non-existent case.

so question is: there way avoid without tediously add possible cases between 0 , 255 hand , still keep explicit cases want refer name, @ same time have possibility of generating random color value between 0 , 255?

after all, code different threw me in right direction. indeed, there no other way turning mapcolor struct twist: kept former enum , renamed rawcolor, can still use explicit color cases , not have remember exact raw values given color in color map. thus, of may have similar problem mine or in future, post here code tested in playground (xcode 8 gm using swift 3.0):

import simd  enum rawcolor:uint8 {     case black = 0     case white = 1     case red = 2     case orange = 7     case yellow = 16     case gray = 30     case darkbrown = 40     case lightbrown = 58     case blue = 59     case green = 67     case lightgreen = 77     case rampblue = 78     case rampgreenblue = 123     case rampbluegreen = 128     case rampdarkgreen = 134     case rampgreen = 142     case ramplightgreen = 166     case rampyellow = 185     case ramplightorange = 202     case ramporange = 220     case rampdarkorange = 231     case rampredorange = 238     case rampred = 255 }  struct mapcolor {     var rawvalue:uint8      init(value:uint8)     {         self.rawvalue = value     }      init(color:rawcolor)     {         self.rawvalue = color.rawvalue     }      static func random() -> mapcolor     {         return mapcolor(value: uint8(abs(random(maximum: 255))))     }      private static func random(maximum:double) -> double     {         let r = int32(int64(arc4random()) - int64(rand_max))         let v = (double(r) / double(rand_max)) * maximum         return v     } }  // specify map color specific value let red = rawcolor.red let mapred = mapcolor(color: red) let value = mapred.rawvalue // value = 2  // random colors in 0..<256 {     let randomcolor = mapcolor.random()     let rawvalue = randomcolor.rawvalue } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -