Let's give each colour a value

0 = green

1 = red

2 = blue

The first person adds together the values for all of the hats except for his own and calls out the colour of the hat with that value (mod 3)

The next person can calculate the value of his own hat by adding together the values for all of the hats except for his own and the one before him (mod 3) his hat has the required value to increase this to equal the value that was called out by the first person.

The third and subsequent people have sufficient information to calculate their own hat's value.

e.g.

red green blue green red

1 0 2 0 1

First person calculates 3 which is 0 mod 3

Second person also calculates 3 which is 0 mod 3 and hence knows his hat is value 0. He calls green

Third person calculates 1 and knows that the hat before him is 0. Therefore his hat must be 2 and he calls blue

Fourth person also calculates 1, knows that the hats before him are green/blue and calculates the colour of his hat.