001package org.opencv.core;
002
003//javadoc:Scalar_
004public class Scalar {
005
006    public double val[];
007
008    public Scalar(double v0, double v1, double v2, double v3) {
009        val = new double[] { v0, v1, v2, v3 };
010    }
011
012    public Scalar(double v0, double v1, double v2) {
013        val = new double[] { v0, v1, v2, 0 };
014    }
015
016    public Scalar(double v0, double v1) {
017        val = new double[] { v0, v1, 0, 0 };
018    }
019
020    public Scalar(double v0) {
021        val = new double[] { v0, 0, 0, 0 };
022    }
023
024    public Scalar(double[] vals) {
025        if (vals != null && vals.length == 4)
026            val = vals.clone();
027        else {
028            val = new double[4];
029            set(vals);
030        }
031    }
032
033    public void set(double[] vals) {
034        if (vals != null) {
035            val[0] = vals.length > 0 ? vals[0] : 0;
036            val[1] = vals.length > 1 ? vals[1] : 0;
037            val[2] = vals.length > 2 ? vals[2] : 0;
038            val[3] = vals.length > 3 ? vals[3] : 0;
039        } else
040            val[0] = val[1] = val[2] = val[3] = 0;
041    }
042
043    public static Scalar all(double v) {
044        return new Scalar(v, v, v, v);
045    }
046
047    public Scalar clone() {
048        return new Scalar(val);
049    }
050
051    public Scalar mul(Scalar it, double scale) {
052        return new Scalar(val[0] * it.val[0] * scale, val[1] * it.val[1] * scale,
053                val[2] * it.val[2] * scale, val[3] * it.val[3] * scale);
054    }
055
056    public Scalar mul(Scalar it) {
057        return mul(it, 1);
058    }
059
060    public Scalar conj() {
061        return new Scalar(val[0], -val[1], -val[2], -val[3]);
062    }
063
064    public boolean isReal() {
065        return val[1] == 0 && val[2] == 0 && val[3] == 0;
066    }
067
068    @Override
069    public int hashCode() {
070        final int prime = 31;
071        int result = 1;
072        result = prime * result + java.util.Arrays.hashCode(val);
073        return result;
074    }
075
076    @Override
077    public boolean equals(Object obj) {
078        if (this == obj) return true;
079        if (!(obj instanceof Scalar)) return false;
080        Scalar it = (Scalar) obj;
081        if (!java.util.Arrays.equals(val, it.val)) return false;
082        return true;
083    }
084
085    @Override
086    public String toString() {
087        return "[" + val[0] + ", " + val[1] + ", " + val[2] + ", " + val[3] + "]";
088    }
089
090}