package jxl.biff;

import jxl.SheetSettings;
import jxl.format.Colour;
import jxl.format.RGB;
import jxl.read.biff.Record;

/* JADX INFO: loaded from: classes.dex */
public class PaletteRecord extends WritableRecordData {
    private static final int numColours = 56;
    private boolean dirty;
    private boolean initialized;
    private boolean read;
    private RGB[] rgbColours;

    public PaletteRecord(Record record) {
        super(record);
        this.rgbColours = new RGB[56];
        this.initialized = false;
        this.dirty = false;
        this.read = true;
    }

    private void initialize() {
        byte[] data = getRecord().getData();
        int i2 = IntegerHelper.getInt(data[0], data[1]);
        for (int i3 = 0; i3 < i2; i3++) {
            int i4 = (i3 * 4) + 2;
            this.rgbColours[i3] = new RGB(IntegerHelper.getInt(data[i4], (byte) 0), IntegerHelper.getInt(data[i4 + 1], (byte) 0), IntegerHelper.getInt(data[i4 + 2], (byte) 0));
        }
        this.initialized = true;
    }

    private int setValueRange(int i2, int i3, int i4) {
        return Math.min(Math.max(i2, i3), i4);
    }

    public RGB getColourRGB(Colour colour) {
        int value = colour.getValue() - 8;
        if (value < 0 || value >= 56) {
            return colour.getDefaultRGB();
        }
        if (!this.initialized) {
            initialize();
        }
        return this.rgbColours[value];
    }

    @Override // jxl.biff.WritableRecordData
    public byte[] getData() {
        if (this.read && !this.dirty) {
            return getRecord().getData();
        }
        byte[] bArr = new byte[226];
        IntegerHelper.getTwoBytes(56, bArr, 0);
        for (int i2 = 0; i2 < 56; i2++) {
            int i3 = (i2 * 4) + 2;
            bArr[i3] = (byte) this.rgbColours[i2].getRed();
            bArr[i3 + 1] = (byte) this.rgbColours[i2].getGreen();
            bArr[i3 + 2] = (byte) this.rgbColours[i2].getBlue();
        }
        return bArr;
    }

    public boolean isDirty() {
        return this.dirty;
    }

    public void setColourRGB(Colour colour, int i2, int i3, int i4) {
        int value = colour.getValue() - 8;
        if (value < 0 || value >= 56) {
            return;
        }
        if (!this.initialized) {
            initialize();
        }
        this.rgbColours[value] = new RGB(setValueRange(i2, 0, SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT), setValueRange(i3, 0, SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT), setValueRange(i4, 0, SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT));
        this.dirty = true;
    }

    public PaletteRecord() {
        super(Type.PALETTE);
        this.rgbColours = new RGB[56];
        this.initialized = true;
        this.dirty = false;
        this.read = false;
        for (Colour colour : Colour.getAllColours()) {
            setColourRGB(colour, colour.getDefaultRGB().getRed(), colour.getDefaultRGB().getGreen(), colour.getDefaultRGB().getBlue());
        }
    }
}
