Negative frequency value's from a color sensor

I am working with an Arduino Uno and a color sensor with the TC320 chip.

Now I wanted to try get the min and the max frequency so I can convert it to RGB with constain and then map, but when I put something black underneath it, it gave negative value's back.

An example ofthe value's:
R: -26138 G: -19033 B: 28900
R: -26710 G: -20529 B: 29528
R: -27482 G: -20357 B: 28762
R: -25933 G: -18918 B: 29128
R: -26849 G: -20511 B: 30372

My code:

#define S0    10
#define S1    9
#define LED   8
#define out   7
#define S2    6
#define S3    5

int R = 0;
int G = 0;
int B = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(out, INPUT);
  pinMode(LED, OUTPUT);

  //set frequency scaling 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
}

void loop() {
  //LED
  digitalWrite(LED, HIGH);
  
  //Red
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  
  R= pulseIn(out, LOW);
  Serial.print("R: ");
  Serial.print(R);

  //Green
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);

  G = pulseIn(out, LOW);
  Serial.print(" G: ");
  Serial.print(G);
  
  //Blue
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);

  B = pulseIn(out, LOW);
  Serial.print(" B: ");
  Serial.println(B);
  
  delay(1000);
}

Does anyone know how to solve this?
Or how to get RGB value's on a different way?

Thx!

int R = 0;
int G = 0;
int B = 0;

The int data type can hold values from -32768 to 32767. 32767 + 1 = -32768 due to "roll over".

R= pulseIn(out, LOW);

The pulseIn() function returns an unsigned long data type. Unsigned long holds 0 to 4,294,967,295. So if pulseIn() returns a value greater than 32767 it rolls over to negative values. Solution, make R, G and B data types appropriate to the values returned to them by pulseIn() (unsigned long).