osx - Objective-C/Mac - Set Pixel Colour? -
in mac application, letting user paint on grid, each square on grid representing pixel , having own colour. possible take colour , simple 1 one produce image setting pixel colour?
i can't think of how else this. need export image once user has painted grid making.
thanks.
the aim of app: user can paint grid. final image might 16x16 pixels in size, user in app can paint each pixel square of grid (16x16). outputted format of final image might 16x16 pixels, literally on screen. while grid visually while painting larger. each square on grid represents pixel in final image, , user can put single colour each square. makes sense, if not shall try , elaborate , link example.
[tldr: use model-view-controller.]
there 3 distinct things consider here:
- the "ideal" image user drawing, grid of 16x16 (or many) coloured pixels.
- the onscreen representation of it, each pixel displayed @ larger size user interact with.
- an eventual image file exported app.
keeping elements separate in mind , implementation make life lot easier.
#1 model, , in case consist of array of 256 (or many) colour values, along descriptive data pixel dimensions , colour space. simple might not want create separate model class (although should), if it's array ivar sitting in monolithic app class should still think of distinct entity in own right.
#2 view (with controller tendencies), , should implement using custom nsview subclass. possible build grid of 256 instances of nscolorwell or other existing nsview type, silly. view has 2 jobs:
- draw model onscreen. iterating on data values , drawing each 1 filled rectangle of appropriate size. methods of
nsbezierpathfriends here, class methodsfillrect,strokerect. - mediate user input. example, might decide user colours pixel clicking on mouse. view sent event each mouse click, , need respond mapping click location correct pixel (basically matter of division) , making appropriate changes model.
#3 in ways kind of view, in way of representing model. code producing included model (say if there's 1 canonical way of serialising data), it's practice put somewhere else, if might want add other arbitrary mechanisms later.
the key here image nothing nsview side of things, it's matter of taking model data , writing out. can writing via nsimage methods or via core graphics, mentioned in other answers, absolutely shouldn't try manhandle directly off screen. way lies madness!
Comments
Post a Comment