[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    branches/KDE/3.5/kdelibs/khtml
From:       Germain Garand <germain () ebooksfrance ! com>
Date:       2006-08-24 16:54:49
Message-ID: 1156438489.158434.14750.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 576708 by ggarand:


Support CSS3's hsv/hsva color values.
cf. http://www.w3.org/TR/2003/CR-css3-color-20030514/#hsla-color

patch by David Carson <dacarson@gmail.com>
ported from WC/-r15151/#9506/



 M  +94 -54    css/cssparser.cpp  
 M  +3 -0      css/cssparser.h  
 M  +29 -0     misc/helper.cpp  
 M  +1 -0      misc/helper.h  


--- branches/KDE/3.5/kdelibs/khtml/css/cssparser.cpp #576707:576708
@@ -1948,6 +1948,70 @@
     return list;
 }
 
+ 
+bool CSSParser::parseColorParameters(Value* value, int* colorArray, bool parseAlpha)
+{
+    ValueList* args = value->function->args;
+    Value* v = args->current();
+    // Get the first value
+    if (!validUnit(v, FInteger | FPercent, true))
+        return false;
+    colorArray[0] = static_cast<int>(v->fValue * (v->unit == \
CSSPrimitiveValue::CSS_PERCENTAGE ? 256.0 / 100.0 : 1.0)); +    for (int i = 1; i < \
3; i++) { +        v = args->next();
+        if (v->unit != Value::Operator && v->iValue != ',')
+            return false;
+        v = args->next();
+        if (!validUnit(v, FInteger | FPercent, true))
+            return false;
+        colorArray[i] = static_cast<int>(v->fValue * (v->unit == \
CSSPrimitiveValue::CSS_PERCENTAGE ? 256.0 / 100.0 : 1.0)); +    }
+    if (parseAlpha) {
+        v = args->next();
+        if (v->unit != Value::Operator && v->iValue != ',')
+            return false;
+        v = args->next();
+        if (!validUnit(v, FNumber, true))
+            return false;
+        colorArray[3] = static_cast<int>(kMax(0.0, kMin(1.0, v->fValue)) * 255);
+    }
+    return true;
+}
+ 
+// CSS3 specification defines the format of a HSL color as
+// hsl(<number>, <percent>, <percent>)
+// and with alpha, the format is
+// hsla(<number>, <percent>, <percent>, <number>)
+// The first value, HUE, is in an angle with a value between 0 and 360
+bool CSSParser::parseHSLParameters(Value* value, double* colorArray, bool \
parseAlpha) +{
+    ValueList* args = value->function->args;
+    Value* v = args->current();
+    // Get the first value
+    if (!validUnit(v, FInteger, true))
+        return false;
+    // normalize the Hue value and change it to be between 0 and 1.0
+    colorArray[0] = (((static_cast<int>(v->fValue) % 360) + 360) % 360) / 360.0;
+    for (int i = 1; i < 3; i++) {
+        v = args->next();
+        if (v->unit != Value::Operator && v->iValue != ',')
+            return false;
+        v = args->next();
+        if (!validUnit(v, FPercent, true))
+            return false;
+        colorArray[i] = kMax(0.0, kMin(100.0, v->fValue)) / 100.0; // needs to be \
value between 0 and 1.0 +    }
+    if (parseAlpha) {
+        v = args->next();
+        if (v->unit != Value::Operator && v->iValue != ',')
+            return false;
+        v = args->next();
+        if (!validUnit(v, FNumber, true))
+            return false;
+        colorArray[3] = kMax(0.0, kMin(1.0, v->fValue));
+    }
+    return true;
+}
 
 static bool parseColor(int unit, const QString &name, QRgb& rgb)
 {
@@ -2015,65 +2079,41 @@
 		value->function->args != 0 &&
                 value->function->args->size() == 5 /* rgb + two commas */ &&
                 qString( value->function->name ).lower() == "rgb(" ) {
-        ValueList *args = value->function->args;
-        Value *v = args->current();
-        if ( !validUnit( v, FInteger|FPercent, true ) )
+        int colorValues[3];
+        if (!parseColorParameters(value, colorValues, false))
             return 0;
-        int r = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? \
                256./100. : 1.) );
-        v = args->next();
-        if ( v->unit != Value::Operator && v->iValue != ',' )
+        colorValues[0] = kMax( 0, kMin( 255, colorValues[0] ) );
+        colorValues[1] = kMax( 0, kMin( 255, colorValues[1] ) );
+        colorValues[2] = kMax( 0, kMin( 255, colorValues[2] ) );
+        c = qRgb(colorValues[0], colorValues[1], colorValues[2]);
+    } else if (value->unit == Value::Function &&
+                value->function->args != 0 &&
+                value->function->args->size() == 7 /* rgba + three commas */ &&
+                domString(value->function->name).lower() == "rgba(") {
+        int colorValues[4];
+        if (!parseColorParameters(value, colorValues, true))
             return 0;
-        v = args->next();
-        if ( !validUnit( v, FInteger|FPercent, true ) )
+        colorValues[0] = kMax( 0, kMin( 255, colorValues[0] ) );
+        colorValues[1] = kMax( 0, kMin( 255, colorValues[1] ) );
+        colorValues[2] = kMax( 0, kMin( 255, colorValues[2] ) );
+        c = qRgba(colorValues[0], colorValues[1], colorValues[2], colorValues[3]);
+    } else if (value->unit == Value::Function &&
+                value->function->args != 0 &&
+                value->function->args->size() == 5 /* hsl + two commas */ &&
+                domString(value->function->name).lower() == "hsl(") {
+        double colorValues[3];
+        if (!parseHSLParameters(value, colorValues, false))
             return 0;
-        int g = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? \
                256./100. : 1.) );
-        v = args->next();
-        if ( v->unit != Value::Operator && v->iValue != ',' )
+        c = khtml::qRgbaFromHsla(colorValues[0], colorValues[1], colorValues[2], \
1.0); +    } else if (value->unit == Value::Function &&
+                value->function->args != 0 &&
+                value->function->args->size() == 7 /* hsla + three commas */ &&
+                domString(value->function->name).lower() == "hsla(") {
+        double colorValues[4];
+        if (!parseHSLParameters(value, colorValues, true))
             return 0;
-        v = args->next();
-        if ( !validUnit( v, FInteger|FPercent, true ) )
-            return 0;
-        int b = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? \
                256./100. : 1.) );
-        r = kMax( 0, kMin( 255, r ) );
-        g = kMax( 0, kMin( 255, g ) );
-        b = kMax( 0, kMin( 255, b ) );
-        c = qRgb( r, g, b );
+        c = khtml::qRgbaFromHsla(colorValues[0], colorValues[1], colorValues[2], \
colorValues[3]);  }
-    else if ( value->unit == Value::Function &&
-              value->function->args != 0 &&
-              value->function->args->size() == 7 /* rgba + three commas */ &&
-              qString( value->function->name ).lower() == "rgba(" ) {
-        ValueList *args = value->function->args;
-        Value *v = args->current();
-        if ( !validUnit( v, FInteger|FPercent, true ) )
-            return 0;
-        int r = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? \
                256./100. : 1.) );
-        v = args->next();
-        if ( v->unit != Value::Operator && v->iValue != ',' )
-            return 0;
-        v = args->next();
-        if ( !validUnit( v, FInteger|FPercent, true ) )
-            return 0;
-        int g = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? \
                256./100. : 1.) );
-        v = args->next();
-        if ( v->unit != Value::Operator && v->iValue != ',' )
-            return 0;
-        v = args->next();
-        if ( !validUnit( v, FInteger|FPercent, true ) )
-            return 0;
-        int b = (int) ( v->fValue * (v->unit == CSSPrimitiveValue::CSS_PERCENTAGE ? \
                256./100. : 1.) );
-        v = args->next();
-        if ( v->unit != Value::Operator && v->iValue != ',' )
-            return 0;
-        v = args->next();
-        if ( !validUnit( v, FNumber, true ) )
-            return 0;
-        r = QMAX( 0, QMIN( 255, r ) );
-        g = QMAX( 0, QMIN( 255, g ) );
-        b = QMAX( 0, QMIN( 255, b ) );
-        int a = (int)(QMAX( 0, QMIN( 1.0f, v->fValue ) ) * 255);
-        c = qRgba( r, g, b, a );
-    }
     else
         return 0;
 
--- branches/KDE/3.5/kdelibs/khtml/css/cssparser.h #576707:576708
@@ -137,6 +137,9 @@
 	bool parseFont(bool important);
         bool parseCounter(int propId, bool increment, bool important);
 
+        bool parseColorParameters(Value*, int* colorValues, bool parseAlpha);
+        bool parseHSLParameters(Value*, double* colorValues, bool parseAlpha);
+
         // returns the found property
         // 0 if nothing found (or ok == false)
         // @param forward if true, it parses the next in the list
--- branches/KDE/3.5/kdelibs/khtml/misc/helper.cpp #576707:576708
@@ -2,6 +2,7 @@
  * This file is part of the CSS implementation for KDE.
  *
  * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
+ *           (C) David Carson  <dacarson@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -45,6 +46,34 @@
 }
 
 
+double calcHue(double temp1, double temp2, double hueVal)
+{
+    if (hueVal < 0)
+        hueVal++;
+    else if (hueVal > 1)
+        hueVal--;
+    if (hueVal * 6 < 1)
+        return temp1 + (temp2 - temp1) * hueVal * 6;
+    if (hueVal * 2 < 1)
+        return temp2;
+    if (hueVal * 3 < 2)
+        return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6;
+    return temp1;
+}
+
+// Explanation of this algorithm can be found in the CSS3 Color Module
+// specification at http://www.w3.org/TR/css3-color/#hsl-color with further
+// explanation available at http://en.wikipedia.org/wiki/HSL_color_space
+
+// all values are in the range of 0 to 1.0
+QRgb khtml::qRgbaFromHsla(double h, double s, double l, double a)
+{
+    double temp2 = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
+    double temp1 = 2.0 * l - temp2;
+
+    return qRgba(calcHue(temp1, temp2, h + 1.0 / 3.0) * 255, calcHue(temp1, temp2, \
h) * 255, calcHue(temp1, temp2, h - 1.0 / 3.0) * 255, a * 255); +}
+
 /** finds out the background color of an element
  * @param obj render object
  * @return the background color. It is guaranteed that a valid color is returned.
--- branches/KDE/3.5/kdelibs/khtml/misc/helper.h #576707:576708
@@ -40,6 +40,7 @@
     
     bool hasSufficientContrast(const QColor &c1, const QColor &c2);
     QColor retrieveBackgroundColor(const RenderObject *obj);
+    QRgb qRgbaFromHsla(double h, double s, double l, double a);
 
     //enumerator for findSelectionNode
     enum FindSelectionResult { SelectionPointBefore,


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic