Quarks have spin 1/2, so are fermions and must obey the Paul Exclusion principle, which states that no two particles in a quantum system can have the same set of quantum numbers.
To solve the problem of quarks occurring in threes in baryons that this seems to present, physicists hypothesised a quantity called color, which came in the varieties - red, blue and green. The Paul Exclusion p Principle applies to each color separately. Each of the the three quarks which made up a baryon had a different color, and when brought together to form the baryon, the combination gave the baryon no color at all.
Color is a conserved quantity. When quarks interact through the exchange of gluons - which carry the strong force, and always consist of a color anticolor pair eg blue antired - the color of the the quarks constituting the baryon change in such a way that there is always a blue, red and green quark in a baryon. The study of color interaction is called quantum chromodynamics or QCD.