# Gradient fills in irregular shapes

**URL:** <https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100>\
**Category:** General\
**Created:** [July 21, 2014, 8:17pm UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100 "2014-07-21T20:17:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Charlesworth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mike\_Charlesworth](https://forum.xojo.com/u/Mike_Charlesworth)\
**Post date:** [July 21, 2014, 8:17pm UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/1 "2014-07-21T20:17:22Z")

</div>

Has anyone written any code for doing gradient fills in polygons or even rounded rectangles. I am quite happy in calculating and filling regular shapes such as a rectangle. I can imagine how to calculate a gradient fill for a circle. My guess would be to calculate lines of varying shade from top to bottom of the circle of varying lengths to fill from left to right of the circle.

Is there an easier way. What about an irregular polygon?

OSX by they way but XPlat would be good.

---

<div class="post-metadata">

**Author:** ![Alain\_Bailleul](https://forum.xojo.com/user_avatar/forum.xojo.com/alain_bailleul/32/1370_2.png) [@Alain\_Bailleul](https://forum.xojo.com/u/Alain_Bailleul)\
**Post date:** [July 21, 2014, 8:35pm UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/2 "2014-07-21T20:35:31Z")

</div>

Can’t you just fill a rectangle with the gradient and then ‘cut’ out the shape you need using the mask?

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [July 21, 2014, 8:40pm UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/3 "2014-07-21T20:40:41Z")

</div>

> [@113686:@Alain Bailleul](#):
>
> Can’t you just fill a rectangle with the gradient and then ‘cut’ out the shape you need using the mask?

That is exactly how you do it…

calculate the min and max X and Y coordinates from your polygon  
create a rectangle picture that is (MaxX-MinX) by (MaxY-MinY) in size  
fill in the MASK with all WHITE, and then draw on the MASK the same Polygon filled with all BLACK  
Then on the main picture surface apply you gradient…

---

<div class="post-metadata">

**Author:** ![Mike\_Charlesworth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mike\_Charlesworth](https://forum.xojo.com/u/Mike_Charlesworth)\
**Post date:** [July 21, 2014, 9:02pm UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/4 "2014-07-21T21:02:00Z")

</div>

[quote=113689:@Dave S]That is exactly how you do it…

calculate the min and max X and Y coordinates from your polygon  
create a rectangle picture that is (MaxX-MinX) by (MaxY-MinY) in size  
fill in the MASK with all WHITE, and then draw on the MASK the same Polygon filled with all BLACK  
Then on the main picture surface apply you gradient…[/quote]  
Sounds like what I need. Great stuff i’ll give it a go. Thanks

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [July 23, 2014, 3:06am UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/5 "2014-07-23T03:06:10Z")

</div>

If you want a fast method for OS X, then take a look at CoreGraphics functions (should be in MacOSLib and MBS), there is a clip to path function and a clip to image function. I would also suggest taking a look at either NSGradient or CGGradient as these will provide smoother and much faster gradients than drawing line by line. Not do mention, the gradient can be done in any direction.

---

<div class="post-metadata">

**Author:** ![Mike\_Charlesworth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mike\_Charlesworth](https://forum.xojo.com/u/Mike_Charlesworth)\
**Post date:** [July 23, 2014, 9:34am UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/6 "2014-07-23T09:34:16Z")

</div>

> [@114026:@Sam Rowlands](#):
>
> If you want a fast method for OS X, then take a look at CoreGraphics functions (should be in MacOSLib and MBS), there is a clip to path function and a clip to image function. I would also suggest taking a look at either NSGradient or CGGradient as these will provide smoother and much faster gradients than drawing line by line. Not do mention, the gradient can be done in any direction.

Thanks Sam I’ll have a dabble with these

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:20pm UTC](https://forum.xojo.com/t/gradient-fills-in-irregular-shapes/18100/7 "2020-10-29T17:20:31Z")

</div>


