Repository: networkx/networkx

Issue:
Gexf export sets the rgba's alpha-value to "None" if missing (gexf schema version 1.1 import)
### Current Behavior
Given I import a gexf-file which has schema version 1.1. There are only rgb values used for color definitions, for example:
`
      <node id="1789" label="1789">
        <ns0:color b="41" g="255" r="0" />...
`

When I export the graph imported before into a gexf file again using networkx (using nx.write_gexf and with default gexf-version '1.2draft') it adds alpha value "None":
`
      <node id="1789" label="1789">
        <viz:color r="0" g="255" b="41" a="None" />...
`

### Expected Behavior
In my opinion it would make more sense to use a default value of _a="1.0"_ to remain compatibility. For example Gephi 0.10.1 can not handle _alpha="None"_ values.

### Steps to Reproduce
You can download the dataset [Schoolday](https://www.michihenninger.ch/youtube/schoolday.gexf) and execute the following code:
`schoolday_graph = nx.read_gexf("data/schoolday.gexf")`
`nx.write_gexf(schoolday_graph, path="data/schoolday_new.gexf")`

Open the saved _schoolday_new.gexf_ file with a text editor. You can see that it uses schema version 1.2 and has every alpha value set to None.

### Current workaround: 
The current workaround I use is to read and write the gexf-file in networkx like described in the reproduce section. Then I open the file with a text editor and replace all _a="None" by _a="1.0"_. 

### Environment
Python version: 3.9.16
NetworkX version: 3.0
Gephi: 0.10.1


Discussion:
Is this a behavior change you noticed between NetworkX versions? In other words, did this work the way you expected in a prior version but changed in 3.0? I don't think the support for different GEXF versions is really that great on the NetworkX side of things, so I wouldn't be surprised if this was something that was just "broken" for a long time.
[Line 572 of networkx/readwrite/gexf.py](https://github.com/networkx/networkx/blob/34d77cf4ed2aa6b69a34f617607d46b572faf45f/networkx/readwrite/gexf.py#L572) is where the difference between version 1.1draft and 1.2draft is processed in NetworkX.

Indeed, if a color is specified and there is no alpha value associated with that color, the value `"None"` will appear in the resulting file.  One could say this is simply enforcing the requirement in v1.2draft that all colors have an alpha value. But I agree that this is somewhat limiting, especially when shifting from older color systems that don't include the alpha value.

One fix would be to change line 572 to provide a default value of 1.0 for the alpha value of the color.  Do we see any down side to making that shift?  I would be fine with a default for alpha and not for red/green/blue because I doubt many people leave out only one of red/green/blue. They might leave out all 3(4 including alpha) but then the "None" value seems like an OK result in that case.

This might be an easy fix.
@rossbar: I never had problems before networkx version 3.0. @dschult did explain it really well. gexf version 1.1 did not support alpha values. Here the Schema of gexf v.1.1 (https://gexf.net/1.1/viz.rng)
```
<!--  new point  -->
<define name="color-content">
<interleave>
<attribute name="r">
<ref name="color-channel"/>
</attribute>
<attribute name="g">
<ref name="color-channel"/>
</attribute>
<attribute name="b">
<ref name="color-channel"/>
</attribute>
</interleave>
</define>
```

 and 1.2 supports alpha values (https://gexf.net/1.2/viz.rng):

```
<define name="color-content">
<interleave>
<attribute name="r">
<ref name="color-channel"/>
</attribute>
<attribute name="g">
<ref name="color-channel"/>
</attribute>
<attribute name="b">
<ref name="color-channel"/>
</attribute>
<optional>
<attribute name="a">
<ref name="alpha-channel"/>
</attribute>
</optional>
```

I think there is no downside to use "1.0" as default when no alpha value is given because when no alpha information is available, I would assume that the color should not be transparent. rgba(245, 40, 145, 1) is the same as rgb(245, 40, 145). Another (maybe even more correct) solution is to simply not set an alpha value when not available because it's marked as optional in the gexf 1.2 schema. I just checked that (I opened a gexf-file of version 1.2draft in a text editor and removed all alpha values and could still open it in Gephi)

I agree that the solution (either use "1.0" as default or simply don't write if not available) should be not a big deal to implement.
@rossbar @dschult : I'm going to make a Pull Request and solve it by simply not setting the alpha value if not available.
@michihenninger Are you still doing this or I should get on it?
Hi @dschult @michihenninger @MridulS If no one's working on this issue, I'd like to take it up!
> Hi @dschult @michihenninger @MridulS If no one's working on this issue, I'd like to take it up!

Working on it. Will soon link a pr ;)
> > Hi @dschult @michihenninger @MridulS If no one's working on this issue, I'd like to take it up!
> 
> Working on it. Will soon link a pr ;)

@Qudirah Thanks a lot. I'm a bit busy these days. It's still on my TODO list ;-)
> > > Hi @dschult @michihenninger @MridulS If no one's working on this issue, I'd like to take it up!
> > 
> > 
> > Working on it. Will soon link a pr ;)
> 
> @Qudirah Thanks a lot. I'm a bit busy these days. It's still on my TODO list ;-)

Anytime!

Resolving patch:
diff --git a/networkx/readwrite/gexf.py b/networkx/readwrite/gexf.py
--- a/networkx/readwrite/gexf.py
+++ b/networkx/readwrite/gexf.py
@@ -569,7 +569,7 @@ def add_viz(self, element, node_data):
                         r=str(color.get("r")),
                         g=str(color.get("g")),
                         b=str(color.get("b")),
-                        a=str(color.get("a")),
+                        a=str(color.get("a", 1.0)),
                     )
                 element.append(e)
 


Test patch:
diff --git a/networkx/readwrite/tests/test_gexf.py b/networkx/readwrite/tests/test_gexf.py
--- a/networkx/readwrite/tests/test_gexf.py
+++ b/networkx/readwrite/tests/test_gexf.py
@@ -491,6 +491,16 @@ def test_missing_viz_attributes(self):
             sorted(e) for e in H.edges()
         )
 
+        # Test missing alpha value for version >draft1.1 - set default alpha value
+        # to 1.0 instead of `None` when writing for better general compatibility
+        fh = io.BytesIO()
+        # G.nodes[0]["viz"]["color"] does not have an alpha value explicitly defined
+        # so the default is used instead
+        nx.write_gexf(G, fh, version="1.2draft")
+        fh.seek(0)
+        H = nx.read_gexf(fh, node_type=int)
+        assert H.nodes[0]["viz"]["color"]["a"] == 1.0
+
         # Second graph for the other branch
         G = nx.Graph()
         G.add_node(0, label="1", color="green")