Well, you use physical equality (==) to compare colors in your hash table. If the colors are structured values ββ(I cannot say from this code), none of them will be physically equal to each other. If all the colors are separate objects, they will all go into a table, which really can be quite a large number of objects. On the other hand, the hash function will be based on the actual color values ββof R, G, B, so there may well be a large number of duplicates. This will mean that your hash buckets will have very long chains. Perhaps some internal function is not tail recursive, so it overflows the stack.
Usually the length of the longest chain will be 2 or 3, so it is not surprising that this error does not occur often.
Looking at my copy of hashtbl.ml (OCaml 3.12.1), I don't see anything non-tail-recursive on line 54. So my hunch might be wrong. At line 54, a new internal array is allocated for the hash table. So, another idea is that your hash table just gets too big (possibly due to unwanted duplicates).
You can try to use structural equality (=) and see if the problem persists.
source share