If you want to work with your actual algorithm:
+ On touch box
++ brick is overlapping box
++ for each brick
+++ justifuns stuff + checking all 4 arround
If you want to do it better you should make a 2 dimensional array, insert into it for each field a number from 1-4 to identify the colors.
If you rotate than a box you just have to rotate it and set the array informations right. This would make it much easier to build all functions that you will need ;)