Latest Uploads Extraterre ... .0.1 (zip)dantheman363Monty Teas ... Screenie 1steve_ancell Santa Clau ... ed his bagwarofficemanic_platdude.pngspinal Tetris Clonesteve_ancellSuper blues bros.spinal
 Code Snippets > Variables ( Created 23 August 2007 | Last Edited 23 August 2007)

 Fast Colour Blending Function by mike_g | Rating 100% Written inBlitz Heres a fast colour blending function using some techniques I was shown over on the DBF boards. I thought I would share it as its something I find it useful. It avoids floating point maths and RGB colour conversions. |edit| Oh, and the alpha component should be a number between 0 and 255 |edit| Function ColourBlend( rgb_1, rgb_2, alpha_1 ) alpha_2 = 256 - alpha_1 rb_1 = ((rgb_1 And \$00FF00FF) * alpha_1) And \$FF00FF00 g_1 = ((rgb_1 And \$0000FF00) * alpha_1) And \$00FF0000 rb_2 = ((rgb_2 And \$00FF00FF) * alpha_2) And \$FF00FF00 g_2 = ((rgb_2 And \$0000FF00) * alpha_2) And \$00FF0000 rb_1 = rb_1 + rb_2 g_1 = g_1 + g_2 Return (rb_1 Or g_1) Sar 8 End Function --> And heres a test prog for it: Global cursor = CreateImage(100, 100) SetBuffer ImageBuffer(cursor) For i = 1 To 10 Color (i*20), (i*2), (i*8) Oval (i*10), (i*10), 100-(i*10), 100-(i*10) , 1 Next SetBuffer BackBuffer() RotateImage cursor, 180 Global x_pos = 10 Global y_pos = 10 Global alpha = 127 While Not KeyHit(1) Cls DrawText() LockBuffer BackBuffer() DrawBackGround() DrawAlphaSprite() UnlockBuffer BackBuffer() KeyInput() Flip False Wend ;---------------------------------------------------------------------; ;************************ COLOUR BLEND *******************************; ;---------------------------------------------------------------------; Function ColourBlend( rgb_1, rgb_2, alpha_1 ) alpha_2 = 256 - alpha_1 rb_1 = ((rgb_1 And \$00FF00FF) * alpha_1) And \$FF00FF00 g_1 = ((rgb_1 And \$0000FF00) * alpha_1) And \$00FF0000 rb_2 = ((rgb_2 And \$00FF00FF) * alpha_2) And \$FF00FF00 g_2 = ((rgb_2 And \$0000FF00) * alpha_2) And \$00FF0000 rb_1 = rb_1 + rb_2 g_1 = g_1 + g_2 Return (rb_1 Or g_1) Sar 8 End Function ;---------------------------------------------------------------------; ;************************ DEMO FUNCTIONS *****************************; ;---------------------------------------------------------------------; Function DrawText() Color 255, 255, 255 Text 280, 20, "MOVE WITH" Text 280, 40, "ARROW KEYS" Text 280, 60, "- AND = ALTERS" Text 280, 80, "ALPHA" End Function Function DrawBackground() For y = 0 To 255 Color 100, 100+(y/2), y Line 20, 20+y, 275, 20+y Next End Function Function DrawAlphaSprite() LockBuffer ImageBuffer(cursor) For y = 1 To 99 draw_y = y+y_pos For x = 1 To 99 col1 = ReadPixelFast(x, y, ImageBuffer(cursor)) If col1 <> -16777216 col2 = ReadPixelFast(x+x_pos, draw_y) WritePixelFast(x+x_pos, draw_y, ColourBlend( col1, col2, alpha )) EndIf Next Next UnlockBuffer ImageBuffer(cursor) End Function Function KeyInput() If KeyDown(203) x_pos = x_pos - 3 If KeyDown(205) x_pos = x_pos + 3 If KeyDown(200) And y_pos > 3 Then y_pos = y_pos - 3 If KeyDown(208) And y_pos < GraphicsHeight()-100 Then y_pos = y_pos + 3 If KeyDown(12) And alpha > 3 Then alpha = alpha -3 If KeyDown(13) And alpha < 253 Then alpha = alpha +3 End Function -->

Posted : Thursday, 23 August 2007, 23:21
shroom_monk

Vote : 5
That looks pretty useful. I could use that in future...

 Latest PostsPosition in Mind Jayenkai Tue 12:14 Fucking Pound Sign Unicode Bullshit Bollocks steve_ancell Tue 11:12 Progress / Location Bars dna Tue 08:55 Screen Burn of the Mind rockford Tue 02:13 RoadRash! Mog Mon 10:56 Noel's Graduation rockford Mon 07:37 Development via GUI waroffice Mon 02:48 Audio Rant steve_ancell Sat 19:16 Wrong Partition!!!!? spinal Sat 11:24 eBay Lies spinal Fri 23:44 More

 Latest Items News : Newsletter #176Jayenkai Sat 04:49 News : Newsletter #175Dabz Tue 09:38 Blog : Snow: More Material JunkCower Sat 23:17 Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversionrockford Fri 13:14 Techy : AppleTVJayenkai Thu 09:40 Blog : Graphvizsteve_ancell Sat 14:17 Pets : Top-Down Shadow HackJayenkai Tue 05:52 Snippet : JNKrunch v1.0Jayenkai Sat 07:20 News : Newsletter #173waroffice Fri 04:47 Blog : Material LoadingCower Fri 02:08 Pets : I Done Won A Thingshroom_monk Sun 11:31 Pets : Repurposing A LexerCower Mon 22:06 Bah : Feeling a Little Angryspinal Mon 11:26 News : Newsletter #170Dabz Sat 00:34 Showcase : sbfgenCower Sat 16:57More

Who's Online
dna
Tue, at 12:22
Jayenkai
Tue, at 12:14
spinal
Tue, at 12:11
Afr0
Tue, at 11:45
steve_ancell
Tue, at 11:24
Cower
Tue, at 09:49
rockford
Tue, at 09:10
Dabz
Tue, at 08:54